2

如何将触发器设置为取决于子属性?就像我想根据ExpanderListView 是否有孩子来更改我的Expander的标题。

但我总是收到一个 Comilor 错误,即 HasItems 无法解决...

<Expander Header="Expand to add new ports">
  <Expander.Resources>
    <Style TargetType="{x:Type Expander}">
      <Style.Triggers>
         <Trigger Property="Content.HasItems" Value="False">
           <Setter Property="Header" Value="No children" />
         </Trigger>
      </Style.Triggers>
    </Style>
  </Expander.Resources>
  <ListView ItemsSource="{Binding Path=SomeItems}">
</ListView>

4

2 回答 2

4

您可以使用DataTrigger绑定到ListViewusing ElementName

<Expander>
    <Expander.Resources>
        <Style TargetType="{x:Type Expander}">
            <Setter Property="Header" Value="Expand to add new ports" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ListView, Path=HasItems}" Value="False">
                    <Setter Property="Header" Value="No children" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Expander.Resources>
    <ListView x:Name="ListView" ItemsSource="{Binding Path=SomeItems}">
    </ListView>
</Expander>

另请注意,如果您在触发器中设置属性以及控件的声明,Setter则不会产生任何效果。

用这个:

<Setter Property="Header" Value="Expand to add new ports" />

而不是这个:

<Expander Header="Expand to add new ports">
于 2012-12-05T11:53:50.630 回答
0

您的代码中有多个问题 -

首先,您不应该set the property in style setter在本地声明它。

DataTrigger是代替Trigger使用。

<Expander>
  <Expander.Style>
    <Style TargetType="{x:Type Expander}">
      <Setter Property="Header" Value="Expand to add new ports"/>
      <Style.Triggers>
         <DataTrigger Binding="{Binding Path=SomeItems.Count}" Value="0">
             <Setter Property="Header" Value="No children" />
         </DataTrigger>
      </Style.Triggers>
    </Style>
  </Expander.Style>
  <ListView ItemsSource="{Binding Path=SomeItems}">
  </ListView>
</Expander>
于 2012-12-05T11:54:36.560 回答