TLDR版本WPF自定义模板列表框中显示了IsSelected
不正确的颜色在一个WPF应用程序,在Windows 10上运行,我有一个列表框与自定义模板,包括文本前景色规范,当一个项目IsSelected成为true。颜色应该是#FFBF00,但显示更轻或“洗出”。如果我将颜色改为其他颜色,我会得到相同的效果(相似的颜色,但更轻)。应用中的其他颜色正确显示(所以它不是显示问题)。
从我发现的内容(请参阅相关问题)中可以看出,这似乎与Windows 8(或相关.NET框架版本)中的更改有关,但我尚未找到解决方法或解决方法。
详细
下面是列表框自定义样式/模板:
<Style x:Key="WorkflowRibbon" TargetType="ListBox">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border ClipToBounds="True">
<ItemsPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<DataTemplate.Resources>
<AlternationConverter x:Key="BackgroundBrushes">
<SolidColorBrush Color="White" Opacity="0.65"/>
<SolidColorBrush Color="White" Opacity="0.45"/>
<SolidColorBrush Color="White" Opacity="0.31"/>
<SolidColorBrush Color="White" Opacity="0.20"/>
<SolidColorBrush Color="White" Opacity="0.10"/>
</AlternationConverter>
<Storyboard x:Key="PhaseSelectedAnimation" Duration="0:0:0.25">
<ColorAnimation Storyboard.TargetProperty="Color"
Storyboard.TargetName="ForegroundBrush"
To="#ffbf00">
<ColorAnimation.EasingFunction>
<PowerEase Power="2"/>
</ColorAnimation.EasingFunction>
</ColorAnimation>
<DoubleAnimation Storyboard.TargetProperty="Scale"
Storyboard.TargetName="RibbonLabel"
To="1.1">
<DoubleAnimation.EasingFunction>
<PowerEase Power="2"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
<Storyboard x:Key="PhaseDeselectedAnimation" Duration="0:0:0.25">
<ColorAnimation Storyboard.TargetProperty="Color"
Storyboard.TargetName="ForegroundBrush">
<ColorAnimation.EasingFunction>
<PowerEase Power="2"/>
</ColorAnimation.EasingFunction>
</ColorAnimation>
<DoubleAnimation Storyboard.TargetProperty="Scale"
Storyboard.TargetName="RibbonLabel">
<DoubleAnimation.EasingFunction>
<PowerEase Power="2"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</DataTemplate.Resources>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="PhaseSelectedStoryboard" Storyboard="{StaticResource PhaseSelectedAnimation}"/>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard x:Name="PhaseDeselectedStoryboard" Storyboard="{StaticResource PhaseDeselectedAnimation}"/>
</DataTrigger.ExitActions>
</DataTrigger>
</DataTemplate.Triggers>
<local:WorkflowRibbonLabel WorkflowPhase="{Binding}"
x:Name="RibbonLabel"
BorderThickness="0"
Scale="1.0"
Background="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex), Converter={StaticResource BackgroundBrushes}}">
<local:WorkflowRibbonLabel.Foreground>
<SolidColorBrush x:Name="ForegroundBrush" Color="#ffffff" Opacity="1"/>
</local:WorkflowRibbonLabel.Foreground>
</local:WorkflowRibbonLabel>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
<Setter Property="AlternationCount" Value="5"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Padding" Value="-1"/>
<Setter Property="SelectionMode" Value="Single"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
</Style.Setters>
</Style>
这种风格在各方面都可以正常使用,比色问题等。需要注意的是这个动画:
<ColorAnimation Storyboard.TargetProperty="Color"
Storyboard.TargetName="ForegroundBrush"
To="#ffbf00">
<ColorAnimation.EasingFunction>
<PowerEase Power="2"/>
</ColorAnimation.EasingFunction>
</ColorAnimation>
获取通过IsSelected触发器调用并不会引起颜色变化(只是错误的颜色):
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="PhaseSelectedStoryboard" Storyboard="{StaticResource PhaseSelectedAnimation}"/>
</DataTrigger.EnterActions>
下面是结果的截图:
下面是它应该样子(只是一个草图,只有黄釉W¯¯字体颜色在这里很重要,忽略了其他方面的差异):
相关问题
- This issue appears to be very similar, if not the same, and indicates that this may only happen on Windows 8+ and not on Windows 7 and earlier.不幸的是我没有一个Windows 7系统测试这个上。
- This appears to be the same issue (possibly by the same person) described on MSDN
- This is a bug file with MS for the issue where it appears to be acknowledged, but with no fix or workaround
那么对于解决方案或解决方法的任何想法?我希望避免编写一个完全自定义的控件(但这可能是它的原因)。