更改 WPF 中样式的 ControlTemplate

发布于 2024-12-29 03:22:58 字数 7074 浏览 1 评论 0原文

MobileApp.Get().Resources.MergedDictionaries.Clear();
Uri uri = new Uri("/Resources/NightModeButton.xaml", UriKind.Relative);
ResourceDictionary resDict = Application.LoadComponent(uri) as ResourceDictionary;

int count = resDict.Values.Count; //count is 1

foreach (object obj in resDict.Values)
{
   Style style = obj as Style;  //NOW I HAVE THE STYLE

   //How do I modify the color of element named "GradientStop0" ???
}
<!-- NightButtonMode.xaml --> 
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">

                                <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="InnerRectangle">
                                                <EasingColorKeyFrame KeyTime="0" Value="#FF9B7C35"/>
                                            </ColorAnimationUsingKeyFrames>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="InnerRectangle">
                                                <EasingColorKeyFrame KeyTime="0" Value="#FF837C35"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>

                            </VisualStateManager.VisualStateGroups>
                            <Rectangle x:Name="InnerRectangle" Margin="8,8,8,8" RadiusY="10" RadiusX="10" StrokeThickness="5">
                                <Rectangle.Effect>
                                    <BlurEffect/>
                                </Rectangle.Effect>
                                <Rectangle.Stroke>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#7F7E7E7E" Offset="1" />
                                        <GradientStop Color="#7F605F5F"  x:Name="GradientStop0"/>
                                    </LinearGradientBrush>
                                </Rectangle.Stroke>
                                <Rectangle.Fill>
                                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                                        <GradientStop Color="White" Offset="0.228"/>
                                        <GradientStop Color="#FFB0BAA1"/>
                                        <GradientStop Color="#FFB0BAA1" Offset="0.823"/>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <Rectangle x:Name="GlassCube" Margin="8,8,8,8" RadiusY="10" RadiusX="10" StrokeThickness="5" Opacity="1">
                                <Rectangle.Stroke>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#7FFFFFFF" />
                                        <GradientStop Color="#7FFFFFFF"/>
                                        <GradientStop Color="#F2FFFFFF" Offset="0.254"/>
                                        <GradientStop Color="#BFFFFFFF" Offset="0.401"/>
                                    </LinearGradientBrush>
                                </Rectangle.Stroke>
                                <Rectangle.OpacityMask>
                                    <RadialGradientBrush>
                                        <GradientStop Color="#FFFBFBFB" Offset="0"/>
                                        <GradientStop Color="White" Offset="1"/>
                                    </RadialGradientBrush>
                                </Rectangle.OpacityMask>
                                <Rectangle.Fill>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="White" Offset="0"/>
                                        <GradientStop Color="#FFFDFDFD"/>
                                        <GradientStop Color="#7FFEFEFE"/>
                                        <GradientStop Color="#FFFDFDFD"/>
                                        <GradientStop Color="#7FFDFDFD" Offset="0.004"/>
                                        <GradientStop Color="#FFFDFDFD"/>
                                        <GradientStop Color="#7FFDFDFD"/>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter x:Name="myContentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsCancel" Value="False"/>
                            <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                            <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                            <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                            <Trigger Property="IsEnabled" Value="False"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

</ResourceDictionary>
MobileApp.Get().Resources.MergedDictionaries.Clear();
Uri uri = new Uri("/Resources/NightModeButton.xaml", UriKind.Relative);
ResourceDictionary resDict = Application.LoadComponent(uri) as ResourceDictionary;

int count = resDict.Values.Count; //count is 1

foreach (object obj in resDict.Values)
{
   Style style = obj as Style;  //NOW I HAVE THE STYLE

   //How do I modify the color of element named "GradientStop0" ???
}
<!-- NightButtonMode.xaml --> 
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">

                                <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="InnerRectangle">
                                                <EasingColorKeyFrame KeyTime="0" Value="#FF9B7C35"/>
                                            </ColorAnimationUsingKeyFrames>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="InnerRectangle">
                                                <EasingColorKeyFrame KeyTime="0" Value="#FF837C35"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>

                            </VisualStateManager.VisualStateGroups>
                            <Rectangle x:Name="InnerRectangle" Margin="8,8,8,8" RadiusY="10" RadiusX="10" StrokeThickness="5">
                                <Rectangle.Effect>
                                    <BlurEffect/>
                                </Rectangle.Effect>
                                <Rectangle.Stroke>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#7F7E7E7E" Offset="1" />
                                        <GradientStop Color="#7F605F5F"  x:Name="GradientStop0"/>
                                    </LinearGradientBrush>
                                </Rectangle.Stroke>
                                <Rectangle.Fill>
                                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                                        <GradientStop Color="White" Offset="0.228"/>
                                        <GradientStop Color="#FFB0BAA1"/>
                                        <GradientStop Color="#FFB0BAA1" Offset="0.823"/>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <Rectangle x:Name="GlassCube" Margin="8,8,8,8" RadiusY="10" RadiusX="10" StrokeThickness="5" Opacity="1">
                                <Rectangle.Stroke>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#7FFFFFFF" />
                                        <GradientStop Color="#7FFFFFFF"/>
                                        <GradientStop Color="#F2FFFFFF" Offset="0.254"/>
                                        <GradientStop Color="#BFFFFFFF" Offset="0.401"/>
                                    </LinearGradientBrush>
                                </Rectangle.Stroke>
                                <Rectangle.OpacityMask>
                                    <RadialGradientBrush>
                                        <GradientStop Color="#FFFBFBFB" Offset="0"/>
                                        <GradientStop Color="White" Offset="1"/>
                                    </RadialGradientBrush>
                                </Rectangle.OpacityMask>
                                <Rectangle.Fill>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="White" Offset="0"/>
                                        <GradientStop Color="#FFFDFDFD"/>
                                        <GradientStop Color="#7FFEFEFE"/>
                                        <GradientStop Color="#FFFDFDFD"/>
                                        <GradientStop Color="#7FFDFDFD" Offset="0.004"/>
                                        <GradientStop Color="#FFFDFDFD"/>
                                        <GradientStop Color="#7FFDFDFD"/>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter x:Name="myContentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsCancel" Value="False"/>
                            <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                            <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                            <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                            <Trigger Property="IsEnabled" Value="False"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

</ResourceDictionary>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2025-01-05 03:22:58

您实际上无法修改模板的某些部分,甚至不要尝试。

如果您需要更改整个地方的某种颜色,请将其声明为资源,并使用DynamicResource在需要的地方引用它,如果您随后将其替换为另一种颜色,则所有内容都应该更新。

例如

<Color x:Key="HighlightColor">Red</Color>
<SolidColorBrush x:Key="HighlightBrush" Color="{DynamicResource HighlightColor}" />
<Border.Background>
    <LinearGradientBrush>
        <GradientStop Offset="0" Color="White" />
        <GradientStop Offset="1" Color="{DynamicResource HighlightColor}" />
    </LinearGradientBrush>
</Border.Background>
Resources["HighlightColor"] = Colors.Green;

You practically cannot modify parts of templates, don't even try.

If you need to change some color all over the place declare it as a resource and reference it where it is needed using DynamicResource, if you then replace it with another color everything should update.

e.g.

<Color x:Key="HighlightColor">Red</Color>
<SolidColorBrush x:Key="HighlightBrush" Color="{DynamicResource HighlightColor}" />
<Border.Background>
    <LinearGradientBrush>
        <GradientStop Offset="0" Color="White" />
        <GradientStop Offset="1" Color="{DynamicResource HighlightColor}" />
    </LinearGradientBrush>
</Border.Background>
Resources["HighlightColor"] = Colors.Green;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文