DataGrid RowStyle - Binding Value in DataTrigger

↘锁芯ラ 提交于 2019-12-08 07:09:10

问题


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?



来源:https://stackoverflow.com/questions/42839104/datagrid-rowstyle-binding-value-in-datatrigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!