全局资源不适用于 xaml 中的控件样式

Posted

技术标签:

【中文标题】全局资源不适用于 xaml 中的控件样式【英文标题】:Global resource does not apply style to control in xaml 【发布时间】:2016-07-08 04:28:55 【问题描述】:

我在App.xaml 中为ProgressBar 声明了以下样式

<Application.Resources>
        <LinearGradientBrush x:Key="ProgressBarBorderBrush"
                     EndPoint="0,1"
                     StartPoint="0,0">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#B2B2B2"
                      Offset="0"/>
                <GradientStop Color="#8C8C8C"
                      Offset="1"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="ProgressBarBackground"
                     EndPoint="1,0"
                     StartPoint="0,0">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#BABABA"
                      Offset="0"/>
                <GradientStop Color="#C7C7C7"
                      Offset="0.5"/>
                <GradientStop Color="#BABABA"
                      Offset="1"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="ProgressBarTopHighlight"
                     StartPoint="0,0"
                     EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#80FFFFFF"
                      Offset="0.05"/>
                <GradientStop Color="#00FFFFFF"
                      Offset="0.25"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="ProgressBarGlassyHighlight"
                     StartPoint="0,0"
                     EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#50FFFFFF"
                      Offset="0.5385"/>
                <GradientStop Color="#00FFFFFF"
                      Offset="0.5385"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="ProgressBarIndicatorGlassyHighlight"
                     StartPoint="0,0"
                     EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#90FFFFFF"
                      Offset="0.5385"/>
                <GradientStop Color="#00FFFFFF"
                      Offset="0.5385"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <RadialGradientBrush x:Key="ProgressBarIndicatorLightingEffectLeft"
                     RadiusX="1"
                     RadiusY="1"
                     RelativeTransform="1,0,0,1,0.5,0.5">
            <RadialGradientBrush.GradientStops>
                <GradientStop Color="#60FFFFC4"
                      Offset="0"/>
                <GradientStop Color="#00FFFFC4"
                      Offset="1"/>
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
        <LinearGradientBrush x:Key="ProgressBarIndicatorLightingEffect"
                     StartPoint="0,1"
                     EndPoint="0,0">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#60FFFFC4"
                      Offset="0"/>
                <GradientStop Color="#00FFFFC4"
                      Offset="1"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <RadialGradientBrush x:Key="ProgressBarIndicatorLightingEffectRight"
                     RadiusX="1"
                     RadiusY="1"
                     RelativeTransform="1,0,0,1,-0.5,0.5">
            <RadialGradientBrush.GradientStops>
                <GradientStop Color="#60FFFFC4"
                      Offset="0"/>
                <GradientStop Color="#00FFFFC4"
                      Offset="1"/>
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>

        <LinearGradientBrush x:Key="ProgressBarIndicatorDarkEdgeLeft"
                     StartPoint="0,0"
                     EndPoint="1,0">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#0C000000"
                      Offset="0"/>
                <GradientStop Color="#20000000"
                      Offset="0.3"/>
                <GradientStop Color="#00000000"
                      Offset="1"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="ProgressBarIndicatorDarkEdgeRight"
                     StartPoint="0,0"
                     EndPoint="1,0">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#00000000"
                      Offset="0"/>
                <GradientStop Color="#20000000"
                      Offset="0.7"/>
                <GradientStop Color="#0C000000"
                      Offset="1"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="ProgressBarIndicatorAnimatedFill" 
                     StartPoint="0,0"
                     EndPoint="1,0">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#00FFFFFF"
                      Offset="0"/>
                <GradientStop Color="#60FFFFFF"
                      Offset="0.4"/>
                <GradientStop Color="#60FFFFFF"
                      Offset="0.6"/>
                <GradientStop Color="#00FFFFFF"
                      Offset="1"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <Style x:Key="x:Type ProgressBar"
       TargetType="x:Type ProgressBar">
            <Setter Property="Foreground"
            Value="#01D328"/>
            <Setter Property="Background"
            Value="StaticResource ProgressBarBackground"/>
            <Setter Property="BorderBrush"
            Value="StaticResource ProgressBarBorderBrush"/>
            <Setter Property="BorderThickness"
            Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="x:Type ProgressBar">
                        <Grid Name="TemplateRoot"
                      SnapsToDevicePixels="true">
                            <Rectangle Fill="TemplateBinding Background"
                               RadiusX="2"
                               RadiusY="2"/>
                            <Border Background="StaticResource ProgressBarGlassyHighlight"
                            Margin="1"
                            CornerRadius="2"/>
                            <Border BorderBrush="#80FFFFFF"
                            Background="StaticResource ProgressBarTopHighlight"
                            BorderThickness="1,0,1,1"
                            Margin="1"/>
                            <Rectangle Name="PART_Track"
                               Margin="1"/>

                            <Decorator x:Name="PART_Indicator"
                               HorizontalAlignment="Left"
                               Margin="1">
                                <Grid Name="Foreground">
                                    <Rectangle x:Name="Indicator"
                                       Fill="TemplateBinding Foreground"/>
                                    <Grid x:Name="Animation" ClipToBounds="true">
                                        <Rectangle x:Name="PART_GlowRect" Width="100" 
                                            Fill="StaticResource ProgressBarIndicatorAnimatedFill"
                                            Margin="-100,0,0,0"
                                            HorizontalAlignment="Left">
                                        </Rectangle>
                                    </Grid>
                                    <Grid x:Name="Overlay">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition MaxWidth="15"/>
                                            <ColumnDefinition Width="0.1*"/>
                                            <ColumnDefinition MaxWidth="15"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <Rectangle x:Name="LeftDark"
                                           Grid.RowSpan="2"
                                           Fill="StaticResource ProgressBarIndicatorDarkEdgeLeft"
                                           RadiusX="1"
                                           RadiusY="1"
                                           Margin="1,1,0,1"/>
                                        <Rectangle x:Name="RightDark"
                                           Grid.RowSpan="2"
                                           Grid.Column="2"
                                           RadiusX="1"
                                           RadiusY="1"
                                           Fill="StaticResource ProgressBarIndicatorDarkEdgeRight"
                                           Margin="0,1,1,1"/>
                                        <Rectangle x:Name="LeftLight"
                                           Grid.Column="0"
                                           Grid.Row="2"
                                           Fill="StaticResource ProgressBarIndicatorLightingEffectLeft"/>
                                        <Rectangle x:Name="CenterLight"
                                           Grid.Column="1"
                                           Grid.Row="2"
                                           Fill="StaticResource ProgressBarIndicatorLightingEffect"/>
                                        <Rectangle x:Name="RightLight"
                                           Grid.Column="2"
                                           Grid.Row="2"
                                           Fill="StaticResource ProgressBarIndicatorLightingEffectRight"/>
                                        <Border x:Name="Highlight1"
                                        Grid.RowSpan="2"
                                        Grid.ColumnSpan="3"
                                        Background="StaticResource ProgressBarIndicatorGlassyHighlight"/>
                                        <Border x:Name="Highlight2"
                                        Grid.RowSpan="2" 
                                        Grid.ColumnSpan="3"
                                        Background="StaticResource ProgressBarTopHighlight"/>
                                    </Grid>
                                </Grid>
                            </Decorator>

                            <Border BorderThickness="TemplateBinding BorderThickness"
                            BorderBrush="TemplateBinding BorderBrush"
                            CornerRadius="2"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Orientation"
                             Value="Vertical">
                                <Setter TargetName="TemplateRoot"
                                Property="LayoutTransform">
                                    <Setter.Value>
                                        <RotateTransform Angle="-90"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>

                            <Trigger Property="IsIndeterminate"
                             Value="true">
                                <Setter TargetName="LeftDark"
                                Property="Visibility"
                                Value="Collapsed"/>
                                <Setter TargetName="RightDark"
                                Property="Visibility"
                                Value="Collapsed"/>
                                <Setter TargetName="LeftLight"
                                Property="Visibility"
                                Value="Collapsed"/>
                                <Setter TargetName="CenterLight"
                                Property="Visibility"
                                Value="Collapsed"/>
                                <Setter TargetName="RightLight"
                                Property="Visibility"
                                Value="Collapsed"/>
                                <Setter TargetName="Indicator"
                                Property="Visibility"
                                Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="IsIndeterminate"
                             Value="false">
                                <Setter TargetName="Animation"
                                Property="Background"
                                Value="#80B5FFA9"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
</Application.Resources>

如您所见,&lt;Style x:Key="x:Type ProgressBar" TargetType="x:Type ProgressBar"&gt; 已被提及,据我所知,这应该适用于应用程序中的所有 ProgressBar

我在Login.xaml 页面下面有ProgressBar,它保存在View 文件夹下。

<ProgressBar Name="LoginProgress" Minimum="0" Maximum="100" HorizontalAlignment="Left" 
 Height="24" Margin="99,124,0,0" VerticalAlignment="Top" Width="109" 
 Style="DynamicResource x:Type ProgressBar">

</ProgressBar>

我还含蓄地提到了 ProgressBar 的 Style 属性。毕竟,这种风格没有得到应用。搜索了谷歌中的所有页面,但无法获得适当的资源来理解,因此在这里。谁能让我知道或提供有关如何完成此操作的更多见解?

【问题讨论】:

您指定的代码有效(只需尝试将其粘贴到新项目中)-您甚至不需要DynamicResource。您是否有一些代码可以加载应用程序资源并覆盖 XAML? No @EliArbel.. 它只是一个新项目,除了这个进度条我还没有写任何东西.. 【参考方案1】:

请参考下面的堆栈溢出链接。这个问题在这里深入讨论。 我想 Dylan 给出的答案应该可以帮到你。

what does x:Key="x:Type TextBox" do?

【讨论】:

以上是关于全局资源不适用于 xaml 中的控件样式的主要内容,如果未能解决你的问题,请参考以下文章

如何在.cs中统一动态修改xaml中style资源定义的样式

是否有任何工具可以为 wpf(xaml)中的复杂控件创建实时样式 [关闭]

如何自定义TimePicker的样式

在代码中解析XAML样式并加载自定义控件

在 Generic.xaml 的模板化控件中应用默认样式

WPF 实现换肤功能