I am developing a control in WPF, my control contains some other WPF controls such as labels, buttons and one DataGrid. So, what I have been trying to do is to create a dependency property that allows me to get the DataSource for my dataGrid from the outside of my container control.
So far I have this code:
public static readonly DependencyProperty dataSourceProperty = DependencyProperty.Register(
"DataSource",
typeof(object),
typeof(MyCustomControl));
public object DataSource
{
get
{
return (object)GetValue(dataSourceProperty);
}
set
{
SetValue(dataSourceProperty, value);
}
}
But what I cant figure out is where I have to write this code:
myDataGrid.ItemsSource = DataSource;
I mean, what I need is, in my xaml file, be able to do this:
<MyCustomControl Name="MyControl" DataSource={Binding MyData}/>
I hope you can help me. Thank you in advance.
After some hours of research I’ve found the solution to my problem:
Since I needed to have access to a nested control dependency property from the container control I did this:
After doing that you can write in you XAML:
You can do this with any nested control dependency property.