WPF根据条件隐藏datagrid行 [英] WPF hide row in datagrid based on condition

查看:851
本文介绍了WPF根据条件隐藏datagrid行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据datagrid中的参数和值隐藏datagrid中的行。我想要做这样的事情;

I need to hide rows in datagrid based on parameters and values in the datagrid. I figured to do something like this;

foreach (System.Data.DataRowView dr in myDataGrid.ItemsSource)
{
   //Logic to determine if Row should be hidden
   if (hideRow == "Yes")
   {
      //Hide row code
   }
}

我只是无法想象如何实际隐藏行。请注意,我不想删除datagrid或项目源的行。

I just cannot figure how to actual hide the row. Please note I don't want to remove the row form the datagrid or the item source.

推荐答案

如果hideRow不是字段(即DataGridRow中不是列):

If hideRow is not a field of the table (i.e. not a column in the DataGridRow):

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding AnyProp, Converter={StaticResource hiddenConverter}}" Value="True">
                <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

并用您的逻辑实现Converter。绑定变量的类型,AnyProp上面将是您的属性类型。 AnyProp可以是行中的任何列。

And realize Converter with your logic. The type of the bound variable, AnyProp above, will be yourPropertyType below. AnyProp could be any of the columns in the row.

[ValueConversion(typeof(yourPropType), typeof(bool))]
public class hiddenConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (hideRow == "Yes")
        {
           return true;
        }
        else
        {
           return false;
        }
    }


}

'value'将是AnyProp,它可以用于确定是否显示行的逻辑,或者该决定可以完全基于其他内容,例如示例中的hideRow。

'value' will be AnyProp, and it can be used in the logic that determines whether or not to show the row, or that decision can be based on something else entirely, such as 'hideRow' in the example.

这篇关于WPF根据条件隐藏datagrid行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆