Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Binding Datagrid Items.Count to Custom Controls Label

Tags:

c#

.net

wpf

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 ?

like image 760
Buko crab Avatar asked Dec 21 '22 21:12

Buko crab


2 Answers

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>
like image 95
Greg Sansom Avatar answered Dec 24 '22 10:12

Greg Sansom


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>
like image 27
Edd Avatar answered Dec 24 '22 11:12

Edd