想象一个可以动态更改主题的wpf应用程序。我通过在应用程序资源级别交换资源字典来实现这一点。theme- resourcedtionary有为TextBox等定义的隐式样式。
现在我有一个部分在我的应用程序中,文本框应该有这个特定的样式"NonDefaultTextBoxStyle",而不是应用程序范围内的隐式。
我想这样做(使用DynamicResource,因为主题可以在运行时更改):
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBox" BasedOn="{DynamicResource NonDefaultTextBoxStyle}"/>
</StackPanel.Resources>
<TextBox .../>
<TextBox .../>
<TextBox .../>
</StackPanel>
而不是这样做:
<StackPanel>
<TextBox Style="{DynamicResource NonDefaultTextBoxStyle}" .../>
<TextBox Style="{DynamicResource NonDefaultTextBoxStyle}" .../>
<TextBox Style="{DynamicResource NonDefaultTextBoxStyle}" .../>
</StackPanel>
现在为了简化这一点,我有这个想法,设置一个可继承的附加属性在StackPanel上,将设置一个指定的样式在每个后代文本框。
这是个好主意吗?有没有更简单的方法?我错过什么了吗?
这几乎可以归结为: BasedOn="{DynamicResource…有什么风格?
我有完全相同的问题,但ItemsContainerStyle…所以我基本上按照你说的做了,写了一个AttachedProperty,允许为BasedOn提供动态资源。
Style BasedOn的动态资源
以下是针对您的情况修改的解决方案:
public class DynamicStyle
{
public static Style GetBaseStyle(DependencyObject obj)
{
return (Style)obj.GetValue(BaseStyleProperty);
}
public static void SetBaseStyle(DependencyObject obj, Style value)
{
obj.SetValue(BaseStyleProperty, value);
}
// Using a DependencyProperty as the backing store for BaseStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BaseStyleProperty =
DependencyProperty.RegisterAttached("BaseStyle", typeof(Style), typeof(DynamicStyle), new UIPropertyMetadata(DynamicStyle.StylesChanged));
public static Style GetDerivedStyle(DependencyObject obj)
{
return (Style)obj.GetValue(DerivedStyleProperty);
}
public static void SetDerivedStyle(DependencyObject obj, Style value)
{
obj.SetValue(DerivedStyleProperty, value);
}
// Using a DependencyProperty as the backing store for DerivedStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DerivedStyleProperty =
DependencyProperty.RegisterAttached("DerivedStyle", typeof(Style), typeof(DynamicStyle), new UIPropertyMetadata(DynamicStyle.StylesChanged));
private static void StylesChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
if (!typeof(FrameworkElement).IsAssignableFrom(target.GetType()))
throw new InvalidCastException("Target must be FrameworkElement");
var Element = (FrameworkElement)target;
var Styles = new List<Style>();
var BaseStyle = GetBaseStyle(target);
if (BaseStyle != null)
Styles.Add(BaseStyle);
var DerivedStyle = GetDerivedStyle(target);
if (DerivedStyle != null)
Styles.Add(DerivedStyle);
Element.Style = MergeStyles(Styles);
}
private static Style MergeStyles(ICollection<Style> Styles)
{
var NewStyle = new Style();
foreach (var Style in Styles)
{
foreach (var Setter in Style.Setters)
NewStyle.Setters.Add(Setter);
foreach (var Trigger in Style.Triggers)
NewStyle.Triggers.Add(Trigger);
}
return NewStyle;
}
}
下面是使用它的一个例子:
<!-- xmlns:ap points to the namespace where DynamicStyle class lives -->
<Button ap:DynamicStyle.BaseStyle="{DynamicResource {x:Type Button}}">
<ap:DynamicStyle.DerivedStyle>
<Style TargetType="Button">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=FirstButtonWarning}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="{DynamicResource WarningBackground}"/>
<Setter Property="Foreground" Value="{DynamicResource WarningForeground}"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ap:DynamicStyle.DerivedStyle>
<TextBlock Text="Button that changes background and foreground when warning is active"/>
</Button>
它真的需要是一个动态资源吗?
也许这对你有帮助
如果没有,这里是一个简单的例子StaticResource
应用程序。xaml
<Application.Resources>
<Style x:Key="myResource" TargetType="Button">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
</Style>
</Application.Resources>
MainWindow.xaml
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Button" BasedOn="{StaticResource myResource}"/>
</StackPanel.Resources>
<Button Content="blubb" Height="23" Width="75" />
</StackPanel>
如果两者都没有帮助你,也许你可以提供你如何添加你的资源
<标题>编辑好的,现在应该回答你的问题了
应用程序。xaml
<Application.Resources>
<SolidColorBrush x:Key="background" Color="Red" />
<SolidColorBrush x:Key="foreground" Color="Blue" />
<Style x:Key="NonDefaultTextBoxStyle" >
<Setter Property="TextBox.Background" Value="{DynamicResource background}"/>
<Setter Property="TextBox.Foreground" Value="{DynamicResource foreground}"/>
</Style>
</Application.Resources>
MainWindow.xaml
<StackPanel>
<Button Content="Button" Height="23" Width="75" Click="Button_Click" />
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource NonDefaultTextBoxStyle}">
</Style>
</StackPanel.Resources>
<TextBox Text="bliii" Height="23" Width="75" />
<TextBox Text="blaaa" Height="23" Width="75" />
<TextBox Text="blubb" Height="23" Width="75" />
</StackPanel>
</StackPanel>
MainWindow.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Resources["background"] = Brushes.Black;
this.Resources["foreground"] = Brushes.Yellow;
}
标题>