可以在 XAML 中选择 AncestorType 的名称

Is possible to sellect the name of AncestorType in XAML

我的 XAML 中有多个列表框。在我有下面这段代码之前:

<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">
                                    <Setter TargetName="Rectangle" Property="Fill" Value="Red"/>
                                </DataTrigger>

可以在我的其他ListBox上引用吗?我将我的第二个列表框命名为:

x:Name="HappyBox"

如何更改上面一行代码以仅引用名称为 "HappyBox"

的列表框

有问题请追问

如果您的控件已命名,则不必使用 AncestorType 指定类型,您只需使用 ElementName.

<DataTrigger Binding="{Binding IsSelected, ElementName=HappyBox}" Value="True">
    <Setter TargetName="Rectangle" Property="Fill" Value="Red"/>
</DataTrigger>

像那样