2008-10-30 145 views
18

我已經創建瞭如下樣式列表框,將有旁邊顯示的一些文字圖像的列表框:更改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'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顯示(文本明智)。

我想要做的就是在列表框中選擇一個項目時,將FontWeight設置爲粗體,將Foreground設置爲白色。

有沒有人遇到過這樣的問題?我已經看過一些相關的問題,但人們已經能夠使用TextBlock來解決我不能遺漏的問題。

任何信息脂肪酶可以給予讚賞。

乾杯

+0

您還可以設置在父控件上附加屬性直接`TextElement.Foreground`。 – Sheridan 2018-03-07 12:17:48

回答

37

也有另一種方式。您可以在ContentPresenter這個屬性

TextBlock.Foreground="YourColour" 

在這種情況下,你也可以用動畫對該財產增加。

+5

這似乎不適用於綁定。 – 2013-08-14 09:08:01

18

這一切都OK,我已成功地回答這個問題我自己,我試圖修改不包含對前景的定義的contentpresenter的前景/ fontWeight設置/ fontWeight設置的所有,我只是需要做是這樣的:

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

即刪除:

TargetName="DisplayText" 
8

基於this related answer,我能夠解決類似的問題有以下幾點:

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