设置网格子属性的网格级别触发器

Grid level triggers that set grid's child properties

是否可以在更新网格子属性的网格级别定义触发器?

我尝试过类似的方法但没有成功:

ViewModel:

public class MyViewModel
{
    public bool PropertyA {get;set;}
    public bool PropertyB {get;set;}
}

(为简洁起见,我省略了所有 PropertyChanged 内容)

XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <TextBlock x:Name="ControlA" Grid.Row="0" />

    <TextBlock x:Name="ControlB" Grid.Row="1" />

    <Grid.Triggers>
        <!-- Condition A -->
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding PropertyA}" Value="false"/>
                <Condition Binding="{Binding PropertyB}" Value="true"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter TargetName="ControlA" Property="Text" Value="Blah"/>
                <Setter TargetName="ControlB" Property="Text" Value="Blah"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
        <!-- Conditon B -->
        <MultiDataTrigger>
             <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding PropertyA}" Value="false"/>
                <Condition Binding="{Binding PropertyB}" Value="false"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter TargetName="ControlA" Property="Text" Value="Blah"/>
                <Setter TargetName="ControlB" Property="Text" Value="Blah"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
        <!-- Condition C -->
        <DataTrigger Binding="{Binding PropertyA}" Value="true">
             <Setter TargetName="ControlA" Property="Text" Value="Blah"/>
             <Setter TargetName="ControlB" Property="Text" Value="Blah"/>
        </DataTrigger>
    </Grid.Triggers>
</Grid>

但是编译器抱怨

The name "ControlA" is not recognized.

The name "ControlB" is not recognized.

The member "Text" is not recognized or is not accessible.

The member "Text" is not recognized or is not accessible.

Is it possible to define triggers at the grid level that update grid's child properties?

没有。设置 SetterTargetName 仅在存在具有匹配名称的元素的 ControlTemplate 中才有意义。

一般来说,一个Setter只能设置一个属性应用样式或触发器的元素。

您应该做的是为 TextBlock(s) 创建一个带有触发器的样式,例如:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid.Resources>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding PropertyA}" Value="false"/>
                        <Condition Binding="{Binding PropertyB}" Value="true"/>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="Text" Value="Blah"/>
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>

    <TextBlock x:Name="ControlA" Grid.Row="0" />

    <TextBlock x:Name="ControlB" Grid.Row="1" />

</Grid>