wpf 更改列表框中内容演示者的前景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/249671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:45:01  来源:igfitidea点击:

Changing the Foreground colour of a contentpresenter in a listbox

wpfforegroundcontentpresenter

提问by Bijington

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.

我必须使用 contentpresenter,因为我正在使用 ListBox 本身的 DisplayMemberPath 过滤显示的内容(以文本方式)。

All I want to do is set the FontWeight to Bold and the Foreground to White when an item is selected in the ListBox.

我想要做的就是在 ListBox 中选择一个项目时将 FontWeight 设置为 Bold 并将 Foreground 设置为 White。

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.

有没有人遇到过这样的问题?我查看了一些相关问题,但人们已经能够使用 TextBlock 来解决他们的问题,但不幸的是我无法解决。

Any info ppl can give will be appreciated.

任何人可以提供的信息将不胜感激。

Cheers

干杯

回答by BrightShadow

There is also another way. You can add in your ContentPresenterthis attribute

还有另一种方式。您可以添加ContentPresenter此属性

TextBlock.Foreground="YourColour"

In this case you can also use animations over that property.

在这种情况下,您还可以在该属性上使用动画。

回答by Bijington

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:

没关系,我已经设法自己回答了这个问题,我试图修改 contentpresenter 的前景/字体权重,它不包含前景/字体权重的定义,我只需要做的是:

<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="White"/>

i.e. remove the:

即删除:

TargetName="DisplayText"

回答by Factor Mystic

Based on this related answer, I was able to solve a similar issue with the following:

基于此相关答案,我能够通过以下方式解决类似问题:

<Setter TargetName="ctContentPresenter" Property="TextBlock.Foreground" Value="{StaticResource StyleForeColorBrush}" />

回答by Anatoly Ruchka

  <Storyboard x:Key="Storyboard1">  
       <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="myContentPresenter">
        <EasingColorKeyFrame KeyTime="0" Value="Black"/>
        <EasingColorKeyFrame KeyTime="0:0:0.2" Value="White"/>
       </ColorAnimationUsingKeyFrames>        
  </Storyboard>