none
wpf datagrid隐藏满足条件的行 RRS feed

答案

  • 您好 zhang_zi_jing,

    >>"如果选择的行对应的数据源项目的A属性为a, 那么隐藏, 如何实现?"

    如果您想要隐藏DataGrid的某些行, 最好的方法是对数据源做筛选,然后再重新绑定数据源到DataGrid控件上。另外一种方法是使用出发器动态的设定行的Visibility属性。这里需要写一个绑定转换器,以下代码供你参考。

    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding A, Converter={StaticResource hiddenConverter}}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    public class HiddenConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.ToString() == "a")
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年5月8日 7:46