首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用触发器更改ListBoxItem.Foreground颜色

问使用触发器更改ListBoxItem.Foreground颜色
EN

Stack Overflow用户
提问于 2012-02-10 01:21:59
回答 1查看 5.3K关注 0票数 1

我有此列表框样式,但由于某些原因,我无法更改基于触发器的前景颜色。当列表框中的项目被选中时,我想将前景色更改为黑色。我到处寻找,所有的解决方案似乎都不适用于我的情况。这是我的列表框。

代码语言:javascript
复制
<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>

任何帮助都将不胜感激!

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-10 01:42:04

您的触发器工作正常,但是我没有看到任何指定当选择一个项目时前景应为黑色的内容...

代码语言:javascript
复制
  <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>

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

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

代码语言:javascript
复制
<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Template">
        ...
    </Setter>
</Style>

和

代码语言:javascript
复制
<Label Text="Name" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=Foreground}"/>

或

代码语言:javascript
复制
<TextBlock Text="Name" />
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9215630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档