问题描述
我为列表框创建了以下样式,该列表框将在某些文本旁边显示图像:
I have created the following style for a listbox that will have an image displayed next to some text:
<Style x:Key="ImageListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter -->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Border">
<Grid Height="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image
x:Name="DisplayImage"
Source="{Binding Path=ThumbnailImage}"
Height="30"
Width="30"
Grid.Column="0"/>
<ContentPresenter
x:Name="DisplayText"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Grid.Column="1"/>
<!--<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black"/>
</Style>
</ContentPresenter.Resources>-->
<!--Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath, Converter={StaticResource myDisplayMemberConverter}}"-->
<!--<Label
x:Name="Text"
Content="{Binding Path=FullNameAndTitle}"
Foreground="Black"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
VerticalContentAlignment="Center"
HorizontalAlignment="Stretch"
Grid.Column="1"
Height="40"/>-->
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<!--<Setter Property="FontWeight" Value="Bold" TargetName="DisplayText"/>-->
<!--<Setter Property="Style" Value="{StaticResource SelectedTextStyle}" TargetName="DisplayText"/>-->
<Setter Property="Background" Value="DarkBlue" TargetName="Border"/>
<Setter Property="Width" Value="40" TargetName="DisplayImage"/>
<Setter Property="Height" Value="40" TargetName="DisplayImage"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Grid>
<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<ScrollViewer Margin="1,1,1,1" Focusable="false" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="true"/>
</ScrollViewer>
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我必须使用 contentpresenter,因为我正在使用 ListBox 本身的 DisplayMemberPath 过滤显示的内容(以文本方式).
I have to use the contentpresenter as I am filtering what is displayed (text wise) using the DisplayMemberPath of the ListBox itself.
我想要做的就是在 ListBox 中选择一个项目时将 FontWeight 设置为 Bold,将 Foreground 设置为 White.
All I want to do is set the FontWeight to Bold and the Foreground to White when an item is selected in the ListBox.
有人遇到过这样的问题吗?我查看了一些相关问题,但人们已经能够使用 TextBlock 来解决他们的问题,但不幸的是我无法解决.
Has anyone encountered a problem like this? I have looked at some related questions but people have been able to use a TextBlock to get around their issues I can't unfortunately.
任何人可以提供的信息将不胜感激.
Any info ppl can give will be appreciated.
干杯
推荐答案
还有另一种方法.你可以在你的 ContentPresenter
这个属性中添加
There is also another way. You can add in your ContentPresenter
this attribute
TextBlock.Foreground="YourColour"
在这种情况下,您还可以在该属性上使用动画.
In this case you can also use animations over that property.
这篇关于更改列表框中内容演示者的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!