WPF - Binding Datagrid Items.Count to Custom Label Controls

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?

+4
source share
2 answers

All you have to do is change the ElementName value to the actual DataGrid name (i.e. dataGrid1 instead of DataGrid).

  <Label Width="30" Height="30" Content="{Binding ElementName=DataGrid, Path=Items.Count}" /> 

Here is a complete working example:

  <Grid> <Grid.Resources> <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=dataGrid1, Path=Items.Count}" /> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources> <StackPanel> <DataGrid Name="dataGrid1" VerticalAlignment="Top" Width="210"> <DataGrid.Columns> <DataGridTextColumn Header="header" /> </DataGrid.Columns> </DataGrid> <local:BindingNavigator x:Name="bindingNavigator1" /> </StackPanel> </Grid> 
+8
source

I found this to work for me ...

  <Style.Triggers> <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0"> <Setter Property="Background"> <Setter.Value> <VisualBrush Stretch="None"> <VisualBrush.Visual> <TextBlock Text="We did't find any matching records for your search..." FontSize="16" FontWeight="SemiBold" Foreground="LightCoral"/> </VisualBrush.Visual> </VisualBrush> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> 
+1
source

All Articles