I am working on a project on WPF and I have to create some user controls. Right now I am developing a navigation bar which allows me to navigate through a datagrid, so in my XAML file I need to pass the datagrid object to the navigation bar, but it is not working.
My navigation bar is the following:
<my:NavigationBar Data="{Binding ElementName=dataGrid1}" HorizontalAlignment="Left" Margin="6,6,0,0" Name="navigationBar1" VerticalAlignment="Top" />
And my data grid is the following:
<DataGrid AutoGenerateColumns="True" Margin="11,46,12,9" Name="dataGrid1" />
And my code behind my navigation bar is the following:
public static readonly DependencyProperty dataProperty =
DependencyProperty.Register("Data",
typeof(DataGrid), typeof(NavigationBar));
private DataGrid dataGrid;
public DataGrid Data
{
get
{ return dataGrid; }
set
{ dataGrid = value; }
}
As you can see, I try to send the control to the navigation bar by doing this:
Data="{Binding ElementName=dataGrid1}"
But when I try to use the dataGrid variable in my code behind, an exception is raised because the dataGrid variable is pointing to null.
So, am I passing incorrectly the control? What am I doing wrong? Is my approach the most appropiate?
Thank you in advance.