更改 WPF 中样式的 ControlTemplate
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上无法修改模板的某些部分,甚至不要尝试。
如果您需要更改整个地方的某种颜色,请将其声明为资源,并使用
DynamicResource
在需要的地方引用它,如果您随后将其替换为另一种颜色,则所有内容都应该更新。例如
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.