我为列表框创建了以下样式,该列表框将在某些文本旁边显示图像:

<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过滤显示的内容(文本方式)。

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

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

任何信息ppl都可以给予赞赏。

干杯

有帮助吗?

解决方案

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

TextBlock.Foreground="YourColour"

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

其他提示

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

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

即。删除:

TargetName="DisplayText"

基于此相关答案,我能够用以下方法解决类似的问题:

<Setter TargetName="ctContentPresenter" Property="TextBlock.Foreground" Value="{StaticResource StyleForeColorBrush}" />
  <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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top