wpf触发Datagrid RowCount

时间:2012-11-21 12:35:01

标签: wpf datagrid rowcount

我有一个定义了一些列的DataGrid。行绑定到ObservableCollection。 Grid旁边是一个应该可见或不可见的按钮,具体取决于行数。当有2行(或更多行)时,它应该是可见的。 我们的想法是使用DataGrid.Rows.Count或DataGrid.Items.Count。

编译器不知道属性“DataGrid.Rows”或“DataGrid.Items”。你知道另一种方式吗?我喜欢只在Xaml中使用它,而不是使用转换器。 (我知道可以用一个计算itemcollection的转换器来实现)有更聪明的方法吗?

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

1 个答案:

答案 0 :(得分:1)

您可以通过在要显示/隐藏的按钮上设置DataTrigger来实现,并绑定到DataGrid上的Items.Count属性。

每当集合中的项目数量达到0或1时,该按钮将被隐藏,当有超过1时,它将再次显示。

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

<强>代码隐藏:

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