Linking property in nested objects in code

The nested ViewModel is set to the DataContext MainWindow:

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

Easy to bind to MySubProperty in XAML:

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

How can I do this binding in code?

// 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)
    {
        // ...
    }
}

I do not have access to MainWindow in MyButton.xaml.cs, so I can not use it as a source.

The button is just an example, but it will be the beginning. In my original scenario, I have no useful dependency property. If dp is required for such a binding, the example will be very useful, including creating dp.

+3
source share
2 answers

How about this? (just a dirty example and untested, should work in principle)

// 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)
    {
        // ...
    }
}

EDIT:

- , :

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

dependency. .

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

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

. , XAML.

0

Source: https://habr.com/ru/post/1764249/


All Articles