gpt4 book ai didi

wpf - 列表框选定文本颜色

转载 作者:行者123 更新时间:2023-12-04 11:38:52 24 4
gpt4 key购买 nike

今天的第二个问题,但希望很简单,我只需要更改列表框控件中所选项目的前景色。

我有以下样式(它会更改选择时的背景但不会更改前景色):

    <Style TargetType="ListBoxItem" x:Key="PinnedListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF9CC164"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF9CC164"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
</Style.Resources>
</Style>

这也行不通

    <Style TargetType="ListBoxItem" x:Key="PinnedListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF9CC164"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF9CC164"/>
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>

我有以下列表框:

                <ListBox Style="{StaticResource PinnedList}" Height="Auto" MaxHeight="200" Visibility="Hidden" HorizontalAlignment="Left" Margin="-8,45,0,0" SelectionChanged="ui_lsbPinnedORGs_SelectionChanged"
SelectedItem="{Binding Path=SelectedPinnedORGsViewModel, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
SelectionMode="Single" ItemsSource="{Binding Path=ORGViewModels, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" MouseEnter="ui_lsbPinnedORGs_MouseEnter"
VerticalAlignment="Top" Width="158" Name="ui_lsbPinnedORGs" ItemContainerStyle="{StaticResource PinnedListBoxItem}">
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<Label Content="{Binding Path=ORG.Acronym, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" />
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox>

最佳答案

你应该使用像这样的基本触发器:

<Style TargetType="ListBoxItem">
...
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Color"/>
</Trigger>
</Style.Triggers>
</Style>

更新

使用当前模板:

<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<Label Content="{Binding}" Foreground="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=Foreground}" />
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>

关于wpf - 列表框选定文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9878389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com