2008-10-30 9 views
18

J'ai créé le style suivant pour une zone de liste qui aura une image affichée à côté du texte:Changer la couleur de premier plan d'un ContentPresenter dans une zone de liste

<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 le ContentPresenter comme je le filtrage ce qui est affiché (en mode texte) en utilisant le DisplayMemberPath du ListBox lui-même.

Tout ce que je veux faire est de mettre le FontWeight en gras et le premier plan en blanc quand un élément est sélectionné dans le ListBox.

Est-ce que quelqu'un a rencontré un problème comme celui-ci? J'ai examiné quelques questions connexes, mais les gens ont pu utiliser un TextBlock pour contourner leurs problèmes, je ne peux malheureusement pas.

Toute information que ppl peut donner sera appréciée.

Vive

+0

Vous pouvez également définir les 'TextElement.Foreground' Fixé directement la propriété sur un contrôle parent. – Sheridan

Répondre

37

Il y a aussi une autre façon. Vous pouvez ajouter dans votre ContentPresenter cet attribut

TextBlock.Foreground="YourColour" 

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

+5

Cela ne semble pas fonctionner avec les liaisons. –

+1

Cela ne fonctionne pas de mon côté – Akanksha

18

Tout est ok, j'ai réussi à répondre à cette question moi-même, je tentais de modifier le premier plan/fontweight du ContentPresenter qui ne contient pas de définition pour le premier plan/fontweight tout ce que je devait simplement n'était-ce:

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

-à-dire enlever la:

TargetName="DisplayText" 
8

Basé sur this related answer, j'ai pu résoudre un problème similaire à ce qui suit:

<Setter TargetName="ctContentPresenter" Property="TextBlock.Foreground" Value="{StaticResource StyleForeColorBrush}" /> 
1
<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> 
Questions connexes