更改列表框中内容呈现器的前景色
我为列表框创建了以下样式,该列表框将在某些文本旁边显示图像:
<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 过滤显示的内容(文本方面)。
我想要做的就是在列表框中选择一个项目时将 FontWeight 设置为 Bold,并将 Foreground 设置为白色。
有人遇到过这样的问题吗? 我查看了一些相关问题,但人们已经能够使用 TextBlock 来解决他们的问题,不幸的是我不能。
ppl 可以提供的任何信息将不胜感激。
干杯
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>
I have to use the contentpresenter as I am filtering what is displayed (text wise) using the DisplayMemberPath of the ListBox itself.
All I want to do is set the FontWeight to Bold and the Foreground to White when an item is selected in the ListBox.
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.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
还有另一种方法。 您可以在
ContentPresenter
中添加此属性。在这种情况下,您还可以在该属性上使用动画。
There is also another way. You can add in your
ContentPresenter
this attributeIn this case you can also use animations over that property.
没关系,我自己已经设法回答这个问题,我试图修改 contentpresenter 的前景/字体粗细,它不包含前景/字体粗细的定义,我只需要做的是:
即删除:
It's all ok, I have managed to answer this question myself, I was trying to modify the foreground/fontweight of the contentpresenter which doesn't contain a definition for foreground/fontweight all i simply needed to do was this:
i.e. remove the:
根据此相关答案,我能够通过以下方法解决类似问题:
Based on this related answer, I was able to solve a similar issue with the following:
我用这种方法来解决我同样的问题
添加文本块然后在其上使用颜色
然后将其设为内容
i use this way to solve my same problem
Adding Textblock and Then Use Color on it
then make it as Content