【问题标题】:Change ListBoxItem.Foreground color using a Trigger使用触发器更改 ListBoxItem.Foreground 颜色
【发布时间】:2012-02-10 03:48:53
【问题描述】:

我有这种列表框样式,但由于某种原因,我无法根据触发器更改前景色。选择列表框中的项目时,我想将前景颜色更改为黑色。我到处搜索,所有解决方案似乎都不适用于我的情况。这是我的列表框...

<ListBox IsSynchronizedWithCurrentItem="True" SelectedValuePath="RecordId" ItemsSource="{Binding People}" SelectedValue="{Binding SelectedPersonId}" HorizontalAlignment="Stretch" Margin="20,5,10,5" Width="Auto" Grid.Row="1" Background="{x:Null}" BorderBrush="{x:Null}" x:Name="lstCustomers">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Height="Auto" Width="Auto" d:DesignWidth="545.375" d:DesignHeight="50.294">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="132.375"/>
                </Grid.ColumnDefinitions>
                <StackPanel x:Name="stackPanel" Orientation="Horizontal" HorizontalAlignment="Stretch">
                    <Label FontSize="16" Content="{Binding FullName}" Foreground="White"/>
                    <Label FontSize="16" Content="{Binding DisplayName}" Margin="5,0,0,0" Foreground="White"/>
                </StackPanel>
                <DockPanel LastChildFill="True" HorizontalAlignment="Stretch" Margin="0" Grid.Column="1">
                    <Label Content="{Binding Date}" ContentStringFormat="yyyy/MM/dd" FontSize="24" HorizontalAlignment="Right" Foreground="White" DockPanel.Dock="Right" VerticalAlignment="Center"/>
                </DockPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border BorderBrush="#26FFFFFF" BorderThickness="3" CornerRadius="5" Name="Border" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#0FFFFFFF"></Setter>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#BEFFFFFF"></Setter>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelected" Value="true"></Condition>
                                    <Condition Property="IsMouseOver" Value="true"></Condition>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

任何帮助将不胜感激!

谢谢!

【问题讨论】:

  • 您想在选择列表框时将标签的前景色更改为黑色吗?
  • 是的,这是正确的。问题是我无法在边框控件上设置前景,所以我不知道如何在引用数据模板中的实际项目的模板中设置样式...

标签: wpf xaml


【解决方案1】:

您的触发器工作正常,但是我没有看到任何指定在选择项目时前景应该是黑色的东西......

  <ControlTemplate TargetType="ListBoxItem">
        <Border BorderBrush="#26FFFFFF" BorderThickness="3" CornerRadius="5" Name="Border" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true">
            <ContentPresenter />
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF" />
                <Setter TargetName="Border" Property="Foreground" Value="Black" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="Border" Property="Background" Value="#0FFFFFFF"></Setter>
                <Setter TargetName="Border" Property="BorderBrush" Value="#BEFFFFFF"></Setter>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Selector.IsSelected" Value="true"></Condition>
                    <Condition Property="IsMouseOver" Value="true"></Condition>
                </MultiTrigger.Conditions>
                <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF"/>
                <Setter TargetName="Border" Property="Foreground" Value="Black" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

我确实看到将LabelForeground 颜色设置为白色的代码。这将覆盖您在 ListBoxItem 样式中设置的任何前景色。

我建议将默认前景色添加到您的ListBoxItem 样式中,并将Label 前景色绑定到ListBoxItem 前景色,或者使用TextBlock 而不是Label,这将通过默认继承前景色。

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Template">
        ...
    </Setter>
</Style>

还有

<Label Text="Name" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=Foreground}"/>

或者

<TextBlock Text="Name" />

【讨论】:

  • 前两个字符用于 Alpha 通道。
  • @Lukasz 是的,WPF 不会像那样读取 Alpha 通道。摆脱它们,它会正常工作。如果要设置透明度,可以使用Opacity 属性
  • 从颜色值中移除 Alpha 通道会破坏我的风格。如果在触发器 IsSelected 上删除 7F (127),则所选项目现在完全是白色的。我尝试做 但它不起作用。你能给我一个例子来说明你将如何解决它吗?此外,如果 WPF 不能那样工作,Blend 怎么会使用 Alpha 通道设置所有颜色?谢谢!
  • @Lukasz 哦,你说得对,我在看别的东西。无论如何,您的触发器很好,但问题是您在 ListBoxItem 内的标签上设置了前景色,这会覆盖ListBoxItem.Foreground 颜色。我更新了我的答案。
  • 我要感谢您在这方面帮助我。我们现在处于同一点。我已尝试执行您刚刚向我展示的操作,但您无法在边界控件上设置前景。我尝试将样式设置为 但效果不佳,所以我被卡住了。我可以对 TextBlock 进行更改并从父级继承白色,但这仍然无助于我在 IsSelected 上设置为黑色...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 2012-09-08
  • 2012-08-05
  • 1970-01-01
  • 2011-01-30
  • 2015-12-09
相关资源
最近更新 更多