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?
public partial class MyButton : Button
{
public MyButton()
{
InitializeComponent();
}
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.
source
share