ListView样式触发器影响一个控件而不影响另一个控件

时间:2014-01-22 16:45:50

标签: wpf listview foreground

我有一个ListView控件,其中包含系统消息列表,以及一个相应的“操作”按钮,可帮助我的用户解析消息:

Example of problem

如果消息是错误消息,我希望文本的前景和操作按钮([更多...]部分)变为红色。正如你所看到的,它并没有这样做。

我正在使用绑定到消息数据的Severity的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>

Button有自己的Style,SC.ActionButtonHack,但我非常小心,不要在样式的任何地方覆盖Foreground:

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

为什么DataTemplate中的TextBlock会响应Trigger中的Foreground更改,而不响应Button? 我需要做什么才能让按钮尊重前景变化?

1 个答案:

答案 0 :(得分:1)

将ContentPresenter的TextElement.Foreground绑定到ListViewItem'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本身上:

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