Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 影响一个控件而不是另一个控件的ListView样式触发器_Wpf_Listview_Foreground - Fatal编程技术网

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

Wpf 影响一个控件而不是另一个控件的ListView样式触发器,wpf,listview,foreground,Wpf,Listview,Foreground,我有一个ListView控件,其中包含一个系统消息列表,以及一个相应的“操作”按钮,可以帮助我的用户解析消息: 如果消息是错误消息,我希望文本的前景和操作按钮(更多…)变为红色。正如你所看到的,它并没有这样做 我正在使用绑定到消息数据严重性的Style.Trigger来设置前台。这适用于DataTemplate中的TextBlock,但无法影响该模板中的按钮。我的XAML如下所示: <ListView x:Name="ImageCenterStatus" Background="{

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

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

我正在使用绑定到消息数据严重性的Style.Trigger来设置前台。这适用于DataTemplate中的TextBlock,但无法影响该模板中的按钮。我的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>

为什么DataTemplate中的TextBlock响应触发器中的前台更改,而不是按钮?
我需要做什么才能使按钮尊重前台更改?

将ContentPresenter的前台绑定到
ListViewItem的前台
,以使其正常工作:

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

将其绑定到按钮本身:

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


你的
SC.ActionButtonHack风格到底是为了什么?用于测试目的。我有一个更复杂的风格,但我是剥离下来,以保持这篇文章简短。这似乎足够公平。这修复了它!非常感谢你。所以为什么样式触发器适用于TextBlock而不适用于按钮?如果查看按钮的默认controlTemplate,您会发现其模板中的前台属性设置了默认值,该属性的优先级高于属性值继承,这就是它不从其父ListViewItem继承的原因。阅读更多关于DP值优先顺序的信息。但是我不是在我的样式中重写默认的ControlTemplate吗?违约怎么可能仍然影响到这一点?