2014-01-10 86 views
0

爲什麼觸發器集合不允許除事件觸發器之外的數據觸發器和其他觸發器?爲什麼觸發器集合不允許數據觸發器

<StackPanel Orientation="Horizontal"> 
    <Label Name="LblHeader" 
          Margin="10,0,10,0" 
          Content="Test" 
          FontWeight="Bold" 
          Foreground="SteelBlue" /> 
    <Button Name="BtnAttach" Content="_Attach"/> 

    <StackPanel.Triggers> 
     <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel }}" Value="False"> 
      <Setter TargetName="LblHeader" Property="Label.Foreground" Value="Gray" /> 
     </DataTrigger> 
    </StackPanel.Triggers> 
</StackPanel> 

回答

1

觸發器可以在四個級別指定....

  1. ControlTemplate級別
  2. DataTemplate水平
  3. Style級別
  4. UIElement水平。

DataTrigger只能在前三個級別工作。理想情況下,它只能用於第二種情況,即StyleDataTemplate

但是,首先你可以澄清你的代碼是什麼意圖?

當StackPanel被禁用時,您是否想將Label文本顯示爲Gray

如果是這樣的IsEnabled屬性是一個可繼承的依賴項屬性,這意味着它將被應用於被禁用的父視覺樹下的所有元素。

因此,如果StackPanel被禁用,那麼它將在Label之下。

在這種情況下一個PropertyTrigger足以如下...

<StackPanel Orientation="Horizontal"> 
    <Label Name="LblHeader" 
      Margin="10,0,10,0" 
      Content="Test" 
      FontWeight="Bold"> 
     <Label.Style> 
     <Style TargetType="Label"> 
      <Setter Property="Foreground" Value="SteelBlue" /> 
      <Style.Triggers> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Foreground" Value="Gray" /> 
      </Trigger> 
      </Style.Triggers> 
     </Style> 
     </Label.Style> 
    </Label> 
    <Button Name="BtnAttach" Content="_Attach"/>   
</StackPanel>