Buko crab Buko crab - 1 month ago 24
C# Question

WPF - Binding Datagrid Items.Count to Custom Controls Label

im new to wpf and try to bind the Items.Count Property of a static defined DataGrid to a Label of my Custom Control.

My current implementation looks like this. But the label stays empty :I

The Class where 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; }
}
}


The XAML of the CustomControl where the Items.Count to be displayed in a label



<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>




the 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 behind EventHandler where i fill the grid and set the DataGrid property of the Custom Control

private void Window_Loaded(object sender, RoutedEventArgs e)
{
dataGrid1.Items.Add("1");
dataGrid1.Items.Add("2");

bindingNavigator1.DataGrid = dataGrid1;
}


Why i cant bind the Items.Count property to the Label ?

Answer

All you need to do is change the value of ElementName to the actual name of the DataGrid (ie dataGrid1 instead of DataGrid).

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

Here is a fully 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>