im new for wpf and try to associate the Items.Count property of the static defined DataGrid with the label of my user control.
My current implementation looks like this. But the label remains blank: I
The class in which the DataGrid is defined:
public class BindingNavigator : Control { private static DataGrid dataGrid; static BindingNavigator() { DefaultStyleKeyProperty.OverrideMetadata(typeof(BindingNavigator), new FrameworkPropertyMetadata(typeof(BindingNavigator))); } public DataGrid DataGrid { set { dataGrid = value; } get { return dataGrid; } } }
XAML CustomControl where Items.Count will be displayed in the shortcut
<Style TargetType="{x:Type local:BindingNavigator}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:BindingNavigator}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid MinWidth="210" MinHeight="50"> <Label Width="30" Height="30" Content="{Binding ElementName=DataGrid, Path=Items.Count}" /> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
XAML where I deploy my custom control
<DataGrid Name="dataGrid1" VerticalAlignment="Top" Width="210"> <DataGrid.Columns> <DataGridTextColumn Header="header" /> </DataGrid.Columns> </DataGrid> <my:BindingNavigator Name="bindingNavigator1" /> </Grid>
The code is for EventHandler, where I fill the grid and set the DataGrid property for the user control
private void Window_Loaded(object sender, RoutedEventArgs e) { dataGrid1.Items.Add("1"); dataGrid1.Items.Add("2"); bindingNavigator1.DataGrid = dataGrid1; }
Why can't I associate the Items.Count property with the label?
source share