代码之家  ›  专栏  ›  技术社区  ›  Bijington

更改列表框中ContentPresenter的前景色

  •  19
  • Bijington  · 技术社区  · 16 年前

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

    <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,因为我正在使用列表框本身的DisplayMemberPath筛选显示的内容(文本)。

    我只想在列表框中选择一个项目时,将fontweight设置为粗体,将前台设置为白色。

    有人遇到过这样的问题吗?我看过一些相关的问题,但是人们可以使用文本块来解决他们的问题,不幸的是,我不能。

    任何PPL能提供的信息都将不胜感激。

    干杯

    4 回复  |  直到 11 年前
        1
  •  38
  •   JJJ Sergey    13 年前

    还有另一种方法。你可以加入你的 ContentPresenter 此属性

    TextBlock.Foreground="YourColour"
    

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

        2
  •  19
  •   Bijington    16 年前

    没关系,我已经自己回答了这个问题,我试图修改ContentPresenter的Foreground/fontWeight,它不包含Foreground/fontWeight的定义,我只需要这样做:

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

    即移除:

    TargetName="DisplayText"
    
        3
  •  8
  •   Community CDub    8 年前

    基于 this related answer ,我能够解决类似的问题,包括:

    <Setter TargetName="ctContentPresenter" Property="TextBlock.Foreground" Value="{StaticResource StyleForeColorBrush}" />
    
        4
  •  2
  •   Anatoly Ruchka    11 年前
      <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>