2014-01-22 33 views
0

我有在它的系統界面列表中的ListView控件,以及相應的「操作」按鈕,這將有助於我的用戶解決信息:ListView控件樣式觸發影響一個控制而不是另一個

Example of problem

如果消息是錯誤消息,我希望文本的前景和動作按鈕([更多...]部分)變爲紅色。正如你所看到的,這不是那樣做的。

我使用綁定到消息數據的嚴重性的Style.Trigger來設置Foreground。這適用於DataTemplate中的TextBlock,但無法影響該模板中的Button。我的XAML看起來像這樣:

<ListView x:Name="ImageCenterStatus" Background="{DynamicResource SC.ControlBrush}" Margin="10,22,10,0" FontSize="12" Grid.Column="1" ClipToBounds="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <WrapPanel> 
         <TextBlock TextWrapping="WrapWithOverflow" cal:Message.Attach="[Event MouseUp] = [Action DoStatusAction($dataContext)]" Text="{Binding Path=DisplayedMessage}"/> 
         <Button x:Name="ActionButton" Content="{Binding Path=DisplayedActionLabel}" FontSize="12" cal:Message.Attach="DoStatusAction($dataContext)" Style="{DynamicResource SC.ActionButtonHack}"></Button> 
        </WrapPanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
      <ListView.ItemContainerStyle> 
       <Style TargetType="ListViewItem"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding Path=Severity}" Value="Warning"> 
          <Setter Property="Foreground" Value="#FFCE7A16" /> 
         </DataTrigger> 
         <DataTrigger Binding="{Binding Path=Severity}" Value="Error"> 
          <Setter Property="Foreground" Value="#FFD13636" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </ListView.ItemContainerStyle> 
     </ListView> 

的按鈕都有自己的風格,SC.ActionButtonHack,但我很小心,不要覆蓋前景的任何地方風格:

  <Style x:Key="SC.ActionButtonHack" TargetType="{x:Type Button}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

爲什麼不將TextBlock DataTemplate響應觸發器中的前臺更改,但不響應按鈕? 我需要做些什麼來讓按鈕尊重前景變化? ContentPresenter的

+0

這到底是你的'SC.ActionButtonHack風格'爲了? – Sheridan

+0

用於測試目的。我有更多的參與風格,但是爲了保持這篇文章的簡短,我正在剝離它。 –

+0

這似乎夠公平。 – Sheridan

回答

1

綁定TextElement.ForegroundListViewItem's foreground得到它的工作:

<Style x:Key="SC.ActionButtonHack" TargetType="{x:Type Button}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <ContentPresenter x:Name="contentPresenter" 
           TextElement.Foreground="{Binding Foreground, 
           RelativeSource={RelativeSource Mode=FindAncestor, 
                AncestorType=ListViewItem}}" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center" Margin="0"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

OR

無論是其綁定在按鈕本身:

<Button x:Name="ActionButton" 
     Foreground="{Binding Foreground, RelativeSource={RelativeSource 
         Mode=FindAncestor, AncestorType=ListViewItem}}" 
     Content="{Binding Path=Name}" FontSize="12" 
     Style="{DynamicResource SC.ActionButtonHack}"/> 
+0

修好了!非常感謝。那麼......爲什麼樣式觸發器對TextBlock而不是Button有效? –

+0

如果您查看按鈕的默認controlTemplate,您會發現它的模板中的前景屬性的默認值設置爲比屬性值繼承具有更高優先級,這就是爲什麼它不會從其父ListViewItem繼承的原因。閱讀有關DP值優先順序[此處]的更多信息(http://msdn.microsoft.com/zh-cn/library/ms743230(v = vs.110).aspx)。 –

+0

但是我不覆蓋我的風格的默認ControlTemplate?默認仍然會如何影響呢? –

相關問題