C# 从另一个类修改依赖项属性

C# 从另一个类修改依赖项属性,c#,wpf,data-binding,dependency-properties,C#,Wpf,Data Binding,Dependency Properties,我目前有以下情况: public static readonly DependencyProperty IsCopyEnabledProperty = DependencyProperty.Register( "IsCopyEnabled", typeof(bool), typeof(MainWindow)); public bool IsCopyEnabled { get { return (bool)G

我目前有以下情况:

public static readonly DependencyProperty IsCopyEnabledProperty =
    DependencyProperty.Register(
        "IsCopyEnabled",
        typeof(bool),
        typeof(MainWindow));

    public bool IsCopyEnabled
    {
        get { return (bool)GetValue(IsCopyEnabledProperty); }
        set { SetValue(IsCopyEnabledProperty, value); }
    }
我将其绑定到我创建的按钮上,以确定是否应启用或禁用它。我通常调用以下命令来更改IsCopyEnabled在其中声明的类中的值:

IsCopyEnabled = !IsCopyEnabled;

我想知道如何在另一个类中更改IsCopyEnabled的值(尽管名称空间相同)。

就像每个类实例的任何其他类属性一样

MainWindow window = new MainWindow();
window.IsCopyEnabled = !window.IsCopyEnabled;
因为您已经为MainWindow注册了DP,所以也可以这样做

Application.Current.MainWindow.IsCopyEnabled = !Application.Current.MainWindow.IsCopyEnabled;

SetValue是一个公共方法。如果有对目标对象的引用,则可以从另一个类调用它:

Button button = new Button();
button.SetValue(Button.IsEnabledProperty, !(bool)button.GetValue(Button.IsEnabledProperty));