Listbox에서 ContentPresenter의 전경 색상 변경
-
05-07-2019 - |
문제
일부 텍스트 옆에 이미지가 표시되는 ListBox의 다음 스타일을 만들었습니다.
<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>
ListBox 자체의 DisplayMemberPath를 사용하여 표시된 내용 (텍스트 현명한)을 필터링 할 때 ContentPresEnter를 사용해야합니다.
내가하고 싶은 것은 ListBox에서 항목을 선택할 때 Fontweight를 대담하고 전경을 흰색으로 설정하는 것입니다.
이런 문제가 발생한 사람이 있습니까? 나는 몇 가지 관련 질문을 살펴 보았지만 사람들은 불행히도 내가 할 수없는 문제를 해결하기 위해 텍스트 블록을 사용할 수있었습니다.
PPL이 줄 수있는 모든 정보는 감사 할 것입니다.
건배
해결책
다른 방법도 있습니다. 당신은 당신의 추가 할 수 있습니다 ContentPresenter
이 속성
TextBlock.Foreground="YourColour"
이 경우 해당 속성에 애니메이션을 사용할 수도 있습니다.
다른 팁
모두 괜찮습니다. 저는이 질문에 직접 대답했습니다. 나는 전경/글꼴에 대한 정의를 포함하지 않는 ContentPresenter의 전경/글을 수정하려고 노력했습니다.
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="White"/>
즉 제거를 제거합니다.
TargetName="DisplayText"
기반 이 관련 답변, 나는 다음과 비슷한 문제를 해결할 수있었습니다.
<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>
제휴하지 않습니다 StackOverflow