【问题标题】:Binding property of static member of Application classApplication类的静态成员的绑定属性
【发布时间】:2018-05-31 20:57:30
【问题描述】:

我有一个类“Application”,它有一个静态成员(MainWindow,继承自 DXWindow),如下所示:

public static MainWindow MainWindowInstance

我实例化并显示它:

MainWindowInstance = new MainWindow();
MainWindowInstance.ShowDialog();

MainWindow 有一个属性:

private Thickness _localAttachmentsButtonsMargin = new Thickness(0, 0, 5, 0);
public Thickness LocalAttachmentsButtonsMargin {
    get {
        return _localAttachmentsButtonsMargin;
    }
    set {
        _localAttachmentsButtonsMargin = value;
        NotifyPropertyChanged("LocalAttachmentsButtonsMargin");
    }
}

我需要从 xaml 中的许多其他控件(如 UserControls)访问 LocalAttachmentsButtonsMargin 属性。

到目前为止我尝试的是:

--> 使用 x:Static: 访问它

Margin="{Binding Path=MainWindowInstance.LocalAttachmentsButtonsMargin, Source={x:Static Application.Current}}"

--> 给 UserControl 一个 x:Name ("UcTmp"),在 UserControl 中提供一个 getter 并从后面的代码中访问该属性

Margin="{Binding ElementName=UcTmp, Path=LocalAttachmentsButtonsMargin}}"

public Thickness LocalAttachmentsButtonsMargin {
        get {
            Application.MainWindowInstance.LocalAttachmentsButtonsMargin;
        }
    }

但它们似乎都不起作用。我想我需要与上面给出的 getter 中的一行代码等效的 xaml。

另一个要求是 MainWindow 中的属性必须是可更新的。理想情况下,如果属性将通过某些代码进行更新,则解决方案能够更新通过 UserControl 表示的视图。

【问题讨论】:

  • MainWindowInstance 真的是属性吗?
  • 不,抱歉。它只是一个静态成员。我已经更新了我的问题。

标签: wpf properties binding static


【解决方案1】:

由于你只能绑定到属性,你应该这样定义MainWindowInstance

public static MainWindow MainWindowInstance { get; set; }

不能是字段:

public static MainWindow MainWindowInstance;

【讨论】:

    猜你喜欢
    • 2014-09-10
    • 1970-01-01
    • 2021-08-26
    • 2012-10-15
    • 2013-08-17
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多