2016-02-25 116 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>