2012-12-05 64 views
2

如何設置觸發器取決於兒童的財產? 就像我想改變擴展取決於是否擴展的ListView 確實有子女或沒有。將觸發器的屬性設置爲兒童

但我總是得到一個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

可以使用ElementName使用綁定到一個ListViewDataTrigger

<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"> 
0

有你的代碼中的多個問題 -

首先,你應該set the property in style setter,而不是在本地聲明它。

二,用DataTrigger代替觸發器。

<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>