Reputation: 784
I have a DataGrid with some columns defined. The rows are bound to an ObservableCollection. Next to the Grid is a button which should be visible or not, depending on the number of rows. It should be visible, when there are 2 (or more) rows. The idea is to use DataGrid.Rows.Count or DataGrid.Items.Count.
The property "DataGrid.Rows" or "DataGrid.Items" are not known by the compiler. Do you know another way ? I like to have it in Xaml only, and not use a Converter for this. (I know that it could be achieved with a converter that counts the itemcollection) Is there a smarter way ?
<Grid>
<DataGrid Name="dg1">
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
<Button Name="btn1" Visibility="Visibility">
</Button>
<Grid.Triggers>
<Trigger SourceName="dg1" Property="DataGrid.Items.Count" Value="0">
<Setter TargetName="btn1" Property="Visibility" Value="Hidden"></Setter>
</Trigger>
<Trigger SourceName="dg1" Property="DataGrid.Items.Count" Value="1">
<Setter TargetName="btn1" Property="Visibility" Value="Hidden"></Setter>
</Trigger>
</Grid.Triggers>
Upvotes: 2
Views: 4613
Reputation: 8907
You can do that by setting a DataTrigger on the button you want to show/hide, and bind to the Items.Count
property on the DataGrid.
Whenever the number of items in the collection reaches 0 or 1, the button will be hidden, and when there are more than 1, it will be shown again.
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<DataGrid Name="dg1" ItemsSource="{Binding MyFooCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" />
<DataGridTextColumn Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
<StackPanel>
<Button Content="Add row" Click="btnAdd" />
<Button Content="Remove row" Click="btnRemove" />
<Button Content="Will be shown/hidden">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count, ElementName=dg1}" Value="0">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding Items.Count, ElementName=dg1}" Value="1">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</StackPanel>
</Window>
Code-behind:
public partial class MainWindow : Window
{
public ObservableCollection<Foo> MyFooCollection { get; set; }
public MainWindow()
{
InitializeComponent();
this.MyFooCollection = new ObservableCollection<Foo>
{
new Foo(1, "Bar1"), new Foo(2, "Bar2"), new Foo(3, "Bar3"),
new Foo(4, "Bar4"), new Foo(5, "Bar5"), new Foo(6, "Bar6")
};
this.DataContext = this;
}
private void btnAdd(object sender, RoutedEventArgs e)
{
MyFooCollection.Add(new Foo(11, "Test1"));
}
private void btnRemove(object sender, RoutedEventArgs e)
{
if (MyFooCollection.Any())
MyFooCollection.RemoveAt(0);
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public Foo(int id, string name)
{
Id = id;
Name = name;
}
}
}
Upvotes: 2