Wpf 从代码隐藏更改样式
我有这种款式Wpf 从代码隐藏更改样式,wpf,xaml,Wpf,Xaml,我有这种款式 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="MainMenuStyle" TargetType="{x:Type TextBlock}"> <Style.Triggers>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MainMenuStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="White"/>
<Setter Property= "FontSize" Value="22"/>
<Setter Property= "FontFamily" Value="Arial"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property= "Foreground" Value="Black" />
<Setter Property= "FontSize" Value="14"/>
<Setter Property= "FontFamily" Value="Verdana"/>
</Trigger>
</Style.Triggers>
</Style>
我必须只使用框架4
谢谢您首次使用后,您无法从MSDN更改您的样式: 当另一个样式基于某个样式时,或当该样式为 第一次申请 在您的例子中,我可能会在XAML中定义另一种样式,并在运行时切换它们 否则,如果您还没有使用它,您可以这样做(使用索引制作一个快速示例)
江雷戈里奥已经解释了为什么这不能直接实现的大部分原因。但是,这里有一个解决方案: 您可以在样式的
Setters
中使用DynamicResource
引用,然后当您需要更改样式时,只需更新资源,而不是样式。举个例子,这可能更有意义:
<!-- Colour Resources -->
<SolidColorBrush x:Key="BlueBrush" Color="Blue"/>
<SolidColorBrush x:Key="RedBrush" Color="Red"/>
<!-- TextBlock Style (References the colour resources) -->
<Style x:Key="MainMenuStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="{DynamicResource BlueBrush}"/>
...
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property= "Foreground" Value="{DynamicResource RedBrush}" />
...
</Trigger>
</Style.Triggers>
</Style>
DynamicResource
将处理其余部分。这是我的最终代码
<!-- Colour Resources -->
<!-- Default values -->
<SolidColorBrush x:Key="MenuItem_Select_Color" Color="Blue"/>
<FontFamily x:Key="MenuItem_Select_Font">Calibri</FontFamily>
<sys:Double x:Key="MenuItem_Select_Font_Size">13</sys:Double>
<SolidColorBrush x:Key="MenuItem_UnSelect_Color" Color="Green"/>
<FontFamily x:Key="MenuItem_UnSelect_Font">Arial Black</FontFamily>
<sys:Double x:Key="MenuItem_UnSelect_Font_Size">12</sys:Double>
<!-- TextBlock Style (References the colour resources) -->
<Style x:Key="MainMenuStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="{DynamicResource MenuItem_Select_Color}"/>
<Setter Property= "FontFamily" Value="{DynamicResource MenuItem_Select_Font}"/>
<Setter Property= "FontSize" Value="{DynamicResource MenuItem_Select_Font_Size}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property= "Foreground" Value="{DynamicResource MenuItem_UnSelect_Color}"/>
<Setter Property= "FontFamily" Value="{DynamicResource MenuItem_UnSelect_Font}"/>
<Setter Property= "FontSize" Value="{DynamicResource MenuItem_UnSelect_Font_Size}"/>
</Trigger>
</Style.Triggers>
</Style>
再见我坐在那里想我知道wpf支持样式绑定的东西+1好主意;-)非常感谢你。
<!-- Colour Resources -->
<SolidColorBrush x:Key="BlueBrush" Color="Blue"/>
<SolidColorBrush x:Key="RedBrush" Color="Red"/>
<!-- TextBlock Style (References the colour resources) -->
<Style x:Key="MainMenuStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="{DynamicResource BlueBrush}"/>
...
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property= "Foreground" Value="{DynamicResource RedBrush}" />
...
</Trigger>
</Style.Triggers>
</Style>
App.Current.Resources["BlueBrush"] = new SolidColorBrush(Colors.Pink);
<!-- Colour Resources -->
<!-- Default values -->
<SolidColorBrush x:Key="MenuItem_Select_Color" Color="Blue"/>
<FontFamily x:Key="MenuItem_Select_Font">Calibri</FontFamily>
<sys:Double x:Key="MenuItem_Select_Font_Size">13</sys:Double>
<SolidColorBrush x:Key="MenuItem_UnSelect_Color" Color="Green"/>
<FontFamily x:Key="MenuItem_UnSelect_Font">Arial Black</FontFamily>
<sys:Double x:Key="MenuItem_UnSelect_Font_Size">12</sys:Double>
<!-- TextBlock Style (References the colour resources) -->
<Style x:Key="MainMenuStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="{DynamicResource MenuItem_Select_Color}"/>
<Setter Property= "FontFamily" Value="{DynamicResource MenuItem_Select_Font}"/>
<Setter Property= "FontSize" Value="{DynamicResource MenuItem_Select_Font_Size}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property= "Foreground" Value="{DynamicResource MenuItem_UnSelect_Color}"/>
<Setter Property= "FontFamily" Value="{DynamicResource MenuItem_UnSelect_Font}"/>
<Setter Property= "FontSize" Value="{DynamicResource MenuItem_UnSelect_Font_Size}"/>
</Trigger>
</Style.Triggers>
</Style>
Application.Current.Resources("MenuItem_Select_Color") = New SolidColorBrush(Colors.DarkBlue)
Application.Current.Resources("MenuItem_UnSelect_Color") = New SolidColorBrush(Colors.Gold)
Application.Current.Resources("MenuItem_Select_Font") = New FontFamily("Broadway")
Application.Current.Resources("MenuItem_UnSelect_Font") = New FontFamily("Lucida Console")
Application.Current.Resources("MenuItem_Select_Font_Size") = 20
Application.Current.Resources("MenuItem_UnSelect_Font_Size") = 30