在MVVM上下文中隐藏满足多个条件的DataGrid中的行

时间:2019-10-10 10:22:26

标签: c# wpf mvvm

我正试图在满足两个条件的DataGrid中隐藏所有行:

  • ViewModel的{​​{1}}设置为true
  • bool ShowSelectedCat { get; },其中Cat.equals(ViewModel.SelectedCat)是特定于一行的字符串

    Cat

我也想尊重MVVM模式。 <DataGridTemplateColumn Header="Category"> <DataGridTemplateColumn.CellTemplate> <DataTemplate DataType="{x:Type ui:LocalImage}"> <TextBlock Text="{Binding Cat}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> ShowSelectedCat更改,并将调用ICommand

我看不到如何针对这些多种情况使用PropertyChangedEventHandler

2 个答案:

答案 0 :(得分:0)

                            <DataGrid.RowStyle>
                                <Style TargetType="DataGridRow">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding _prop1}" Value="value1">
                                            <Setter Property="Visibility" Value="Collapsed"></Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding _prop2}" Value="value2">
                                            <Setter Property="Visibility" Value="Collapsed"></Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </DataGrid.RowStyle>

答案 1 :(得分:0)

第一个条件很简单:

<Condition Binding="{Binding DataContex.ShowSelectedCat, RelativeSource={RelativeSource AncestorType=DataGrid}}" Value="True" />

第二个不能用XAML表示。您可以使用转换器和绑定到<MultiBinding>ViewModel.SelectedCat的{​​{1}}并进行比较:

Cat

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding DataContext.ShowSelectedCat, RelativeSource={RelativeSource AncestorType=DataGrid}}" Value="True" />
        <Condition Value="True">
            <Condition.Binding>
                <MultiBinding>
                    <MultiBinding.Converter>
                        <local:MultiConverter />
                    </MultiBinding.Converter>
                    <Binding Path="DataContext.SelectedCat" RelativeSource="{RelativeSource AncestorType=DataGrid}" />
                    <Binding Path="Cat" />
                </MultiBinding>
            </Condition.Binding>
        </Condition>
    </MultiDataTrigger.Conditions>
    <Setter ... />
</MultiDataTrigger>