Подтвердить что ты не робот

Изменение цвета переднего плана contentpresenter в списке

Я создал следующий стиль для списка, в котором будет отображаться изображение рядом с некоторым текстом:

<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 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, поскольку я фильтрую отображаемое (текстовое) с помощью DisplayMemberPath самого ListBox.

Все, что я хочу сделать, это установить FontWeight в Bold и Foreground на White, когда элемент выбран в ListBox.

Кто-нибудь сталкивался с такой проблемой? Я рассмотрел некоторые связанные вопросы, но люди смогли использовать TextBlock, чтобы обойти свои проблемы, которых я, к сожалению, не могу.

Любая информация, которую может предоставить ppl, будет оценена.

Приветствия

4b9b3361

Ответ 1

Все в порядке, мне удалось ответить на этот вопрос сам, я пытался изменить передний/средний размер contentpresenter, который не содержит определения для foreground/fontweight, все, что мне просто нужно было сделать:

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

то есть. удалите:

TargetName="DisplayText"

Ответ 2

Есть и другой способ. Вы можете добавить в свой ContentPresenter этот атрибут

TextBlock.Foreground="YourColour"

В этом случае вы также можете использовать анимацию над этим свойством.

Ответ 3

Основываясь на этом связанном ответе, я смог решить аналогичную проблему со следующим:

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

Ответ 4

  <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>