2016-02-25 113 views
1

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> 

下面是结果的截图:

Washed out color, as displayed

下面是它应该样子(只是一个草图,只有黄釉W¯¯字体颜色在这里很重要,忽略了其他方面的差异):

The correct color

相关问题

那么对于解决方案或解决方法的任何想法?我希望避免编写一个完全自定义的控件(但这可能是它的原因)。

回答

0

我想通了。 Windows 8的东西是一个红色的鲱鱼。关键片段是在这里:

<Storyboard x:Key="PhaseDeselectedAnimation" Duration="0:0:0.25"> 
          <ColorAnimation Storyboard.TargetProperty="Color" 
              Storyboard.TargetName="ForegroundBrush"> 
           <ColorAnimation.EasingFunction> 
            <PowerEase Power="2"/> 
           </ColorAnimation.EasingFunction> 
          </ColorAnimation> 

我假定ColorAnimation将继承其Duration从父Storyboard。这个假设是不正确的。显然ColorAnimation将恢复到默认的Duration(1s,我相信)。因此,当整体动画完成并执行HoldEnd时,ColorAnimation仅完成了其转换为最终颜色的部分过程,这就是为什么它会显示另一种颜色“恰好”偏离所需颜色。

解决这个问题额为简单明确地设置Duration财产上的ColorAnimation,像这样:

<Storyboard x:Key="PhaseDeselectedAnimation" Duration="0:0:0.25"> 
          <ColorAnimation Storyboard.TargetProperty="Color" 
              Duration="0:0:0.25" 
              Storyboard.TargetName="ForegroundBrush"> 
           <ColorAnimation.EasingFunction> 
            <PowerEase Power="2"/> 
           </ColorAnimation.EasingFunction> 
          </ColorAnimation>