【问题标题】:Hide DataGrid row with specific Value C# WPF隐藏具有特定值 C# WPF 的 DataGrid 行
【发布时间】:2020-12-08 20:57:42
【问题描述】:

我有一个 xml 文件,他们有一个属性“Active = true”。如果我删除客户,它将“活动”设置为 false,但客户仍应在我的 xml 文件中。我只是想隐藏“活动”行为假的 DataGrid 列。所以每个“active = false”的客户都不应该显示在我的数据网格中。我希望你明白我想做什么:P

我想过这样的事情:

private void HideCustomer()
        {
            if (active == false)
            {
                DataGrid.HideRow ???? // So if the customer has this attribute set to "false" the row 
            }                         // should be hidden in the DataGrid
        }

【问题讨论】:

  • “我有一个 xml 文件”。您是在使用 xmldataprovider 还是将数据转换为对象?
  • 我有文本框,例如,我可以在其中输入姓名和姓氏,当我按下保存键时,我使用 xmlserializer 编写 xml 文件。我的 datagrid itemssource 是我的客户列表。我希望这就是您想要的。

标签: c# wpf datagrid hide


【解决方案1】:

您可以在 XAML 标记中使用 DataTrigger 定义 RowStyle

<DataGrid ...>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsActive}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

这要求IsActive 是一个公共属性。您还应该实现INotifyPropertyChanged 来发出更改通知。

【讨论】:

    猜你喜欢
    • 2011-04-24
    • 2010-09-30
    • 2012-01-13
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2014-11-30
    相关资源
    最近更新 更多