Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF自定义模板列表框在IsSelected上显示不正确的颜色_C#_Wpf_Colors_Listbox - Fatal编程技术网

C# WPF自定义模板列表框在IsSelected上显示不正确的颜色

C# WPF自定义模板列表框在IsSelected上显示不正确的颜色,c#,wpf,colors,listbox,C#,Wpf,Colors,Listbox,TLDR版本 在Windows 10上运行的WPF应用程序中,我有一个带有自定义模板的列表框,其中包括当一个项目被选中时文本前景色的规范。颜色应为#FFBF00,但显示更浅或“褪色”。如果我将颜色更改为其他颜色,我会得到相同的效果(类似的颜色,但更亮)。应用程序中的其他颜色显示正确(因此这不是显示问题) 根据我的发现(请参阅相关问题),这似乎与Windows 8(或相关的.NET framework版本)中的更改有关,我还没有找到解决方案或解决方法 详细信息 以下是列表框的自定义样式/模板: &

TLDR版本

在Windows 10上运行的WPF应用程序中,我有一个带有自定义模板的列表框,其中包括当一个项目被选中时文本前景色的规范。颜色应为#FFBF00,但显示更浅或“褪色”。如果我将颜色更改为其他颜色,我会得到相同的效果(类似的颜色,但更亮)。应用程序中的其他颜色显示正确(因此这不是显示问题)

根据我的发现(请参阅相关问题),这似乎与Windows 8(或相关的.NET framework版本)中的更改有关,我还没有找到解决方案或解决方法

详细信息

以下是列表框的自定义样式/模板:

<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>

下面是它的外观(只是一个草图,这里只有黄色字体颜色很重要,忽略其他差异):

相关问题

  • 不幸的是,我没有Windows7系统来测试这一点

那么,有什么解决方案或变通办法吗?我希望避免编写一个完全自定义的控件(但归根结底可能就是这样)。Windows8的东西是一个危险的东西。关键代码段如下所示:

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

我想出来了。Windows8的东西是一个危险的东西。关键代码段如下所示:

<Storyboard x:Key="PhaseDeselectedAnimation" Duration="0:0:0.25">
                            <ColorAnimation Storyboard.TargetProperty="Color"
                                            Storyboard.TargetName="ForegroundBrush">
                                <ColorAnimation.EasingFunction>
                                    <PowerEase Power="2"/>
                                </ColorAnimation.EasingFunction>
                            </ColorAnimation>
<Storyboard x:Key="PhaseDeselectedAnimation" Duration="0:0:0.25">
                            <ColorAnimation Storyboard.TargetProperty="Color"
                                            Storyboard.TargetName="ForegroundBrush">
                                <ColorAnimation.EasingFunction>
                                    <PowerEase Power="2"/>
                                </ColorAnimation.EasingFunction>
                            </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>