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
那麼對於解決方案或解決方法的任何想法?我希望避免編寫一個完全自定義的控件(但這可能是它的原因)。