绑定到后面的代码中的嵌套对象中的属性

时间:2010-09-10 22:47:41

标签: c# wpf data-binding dependency-properties code-behind

嵌套的ViewModel设置为MainWindow DataContext:

var mainWindow = new MainWindow();
mainWindow.Show();
mainWindow.DataContext = new
{
    MyProperty = new
    {
        MySubProperty = "Hello"
    }
}

在XAML中很容易绑定到MySubProperty:

<Button Content="{Binding MyProperty.MySubProperty}"/>

如何在代码中执行此绑定?

// MyButton.xaml.cs
public partial class MyButton : Button
{
    public MyButton()
    {
        InitializeComponent();
        // todo: add binding here
    }

    // I want this method called if this datacontext is set.
    // and called if MySubProperty changes and INotifyPropertyChange is implemented in the Datacontext.
    public void MySubPropertyChanged(string newValue)
    {
        // ...
    }
}

我无法访问MyButton.xaml.cs中的MainWindow,因此我无法将其用作源代码。

Button只是一个例子,但它将是一个开始。 在我原来的场景中,我没有有用的依赖属性。如果这样的绑定需要一个dp,那么一个例子将非常有用,包括创建一个dp。

2 个答案:

答案 0 :(得分:2)

这个怎么样? (只是一个肮脏的例子和未经测试的,原则上应该工作)

// MyButton.xaml.cs
public partial class MyButton : Button
{
    public MyButton()
    {
        InitializeComponent();
        this.DataContextChanged += DataContext_Changed;
    }

    private void DataContext_Changed(Object sender,DependencyPropertyChangedEventArgs e)
    {
       INotifyPropertyChanged notify = e.NewValue as INotifyPropertyChanged;
       if(null != notify)
       {
          notify.PropertyChanged += DataContext_PropertyChanged;
       }
    }

    private void DataContext_PropertyChanged(Object sender,PropertyChangedEventArgs e)   
    {
        if(e.PropertyName == "MySubProperty")
           MySubPropertyChanged((sender as YourClass).MySubProperty);
    } 

    public void MySubPropertyChanged(string newValue)
    {
        // ...
    }
}

编辑:

用于绑定代码隐藏中的内容,您可以使用:

Binding binding = new Binding();
// directly to myproperty
binding.Source = MyProperty;
binding.Path = new PropertyPath("MySubProperty");
// or window
binding.Source = mainWindow; // instance
binding.Path = new PropertyPath("MyProperty.MySubProperty");

// then wire it up with (button is your MyButton instance)
button.SetBinding(MyButton.MyStorageProperty, binding);
//or
BindingOperations.SetBinding(button, MyButton.MyStorageProperty, binding);

答案 1 :(得分:0)

在我原来的问题中,我没有依赖属性。所以我创建了一个。

public partial class MyButton : Button
{
    //...

    public string MyStorage
    {
        get { return (string)GetValue(MyStorageProperty); }
        set { SetValue(MyStorageProperty, value); }
    }

    public static DependecyProperty MyStorageProperty = 
        DependencyProperty.Register("MyStorage", typeof(string), typeof(MyButton),
            new UIPropertyMetadata(OnMyStorageChanged));
    public static void OnMyStorageChanged(DependecyObject d, DependencyPropertyChangedEventArgs e)
    {
        var myButton = d as MyButton;
        if (myButton == null)
            return;
        myButton.OnMyStorageChanged(d,e);
    }

    public void OnMyStorageChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        // ...
    }
}

现在我可以在XAML中设置Binding

<local:MyButton MyStorage="{Binding MyProperty.MySubProperty}"/>

这解决了我的问题。但我仍然很好奇如何在没有XAML的情况下进行此绑定。