DataGrid RowStyle - Binding Value in DataTrigger

2020-07-24 06:31发布

问题:

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

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.

回答1:

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


回答2:

Where does 'CreatingUser' sits? in row DataContext(your item), or in ViewModel Behind DataGrid, Or in ViewModel behind Window?

maybe thats your problem?