DataGrid RowStyle-DataTrigger中的绑定值 [英] DataGrid RowStyle - Binding Value in DataTrigger

查看:171
本文介绍了DataGrid RowStyle-DataTrigger中的绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个 RowStyle ,它会根据两个条件(OR)来更改行的 Visibility

I want to build a RowStyle which changes the Visibility of the Rows, depending on two conditions (OR).

默认情况下,所有行都应折叠,无论布尔值(在ViewModel中)设置为 True ,还是可见的或 DataTable 中绑定到 Datagrid 的值等于当前用户。因此,当前用户当然也是属性。

Per default all Rows should be collapsed an be visible whether a Boolean (in ViewModel) is set to True OR a value in the DataTable, bound to the Datagrid, equals to the current User. So, the current user is, of course, a Property too.

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Visibility" Value="Collapsed" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor},Path=DataContext.ColleaguesVisible}" Value="True">
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
            <DataTrigger Binding="{Binding CreatingUser}" Value="{Binding CurrentStaffMember}">
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

但是在值绑定时会出现错误...

But at the Value Binding there's the error...

我已经搜索过了,但是找不到解决这个问题的方法。

I already searched around, but I couldn't find a solution for this problem.

我希望有人可以帮助我。

I hope someone can help me.

推荐答案

不能 Value 属性绑定到 DataTrigger 到某种东西,因为它不是依赖项属性。

You can't bind the Value property to a DataTrigger to something because it is not a dependency property.

您可以做的是使用转换器:

What you could do is to use a converter:

<DataGrid ... x:Name="dgr" xmlns:local="clr-namespace:WpfApp2">
    <DataGrid.Resources>
        <local:Converter x:Key="conv" />
    </DataGrid.Resources>
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor},Path=DataContext.ColleaguesVisible}" Value="True">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=., Converter={StaticResource conv}}" Value="True">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>







namespace WpfApp2
{
    public class Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DataRowView drv = value as DataRowView;
            if(drv != null)
            {
                return drv["CreatingUser"].ToString() == drv["CurrentStaffMember"].ToString();
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

这篇关于DataGrid RowStyle-DataTrigger中的绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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