Question

J'ai créé le style suivant pour une liste déroulante qui affichera une image à côté d'un texte:

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

Je dois utiliser contentpresenter car je filtre ce qui est affiché (en mode texte) à l'aide de DisplayMemberPath du ListBox lui-même.

Tout ce que je veux faire est de définir FontWeight sur Gras et l'avant-plan sur blanc lorsqu'un élément est sélectionné dans le contrôle ListBox.

Quelqu'un at-il rencontré un problème comme celui-ci? J'ai examiné des questions connexes, mais des personnes ont pu utiliser un TextBlock pour résoudre leurs problèmes, ce que je ne peux malheureusement pas.

Toute information que vous pouvez donner sera appréciée.

A bientôt

Était-ce utile?

La solution

Il y a aussi un autre moyen. Vous pouvez ajouter dans votre ContentPresenter cet attribut

TextBlock.Foreground="YourColour"

Dans ce cas, vous pouvez également utiliser des animations sur cette propriété.

Autres conseils

Tout va bien, j'ai réussi à répondre moi-même à cette question. J'essayais de modifier le premier plan / poids de la police de contentpresenter qui ne contenait pas de définition de premier plan / police de poids. p>

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

i.e. enlever le:

TargetName="DisplayText"

Basé sur la cette réponse associée , je a pu résoudre un problème similaire avec les éléments suivants:

<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>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top