设置本地隐式样式,不同于主题样式/替代为BasedOn DynamicResource

Mar*_*ter 6 wpf xaml styles

想象一下我可以动态改变主题的wpf应用程序.我这样做是通过在Application-resource级别交换ResourceDictionaries.主题资源为TextBox等定义了隐式样式.

现在我在我的应用程序中有一个部分,其中文本框应该具有此特定样式"NonDefaultTextBoxStyle"而不是应用程序范围的隐式文本框.

我很乐意这样做(使用DynamicResource,因为主题可以在运行时更改):

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBox" BasedOn="{DynamicResource NonDefaultTextBoxStyle}"/>
    </StackPanel.Resources>
    <TextBox .../>
    <TextBox .../>
    <TextBox .../>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

而不是必须这样做:

<StackPanel>
    <TextBox Style="{DynamicResource NonDefaultTextBoxStyle}" .../>
    <TextBox Style="{DynamicResource NonDefaultTextBoxStyle}" .../>
    <TextBox Style="{DynamicResource NonDefaultTextBoxStyle}" .../>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

现在为了简化这一点,我有了在StackPanel上设置可继承的附加属性的想法,该属性将在每个后代文本框上设置指定的样式.

这是一个好主意吗?有更简单的方法吗?我错过了什么吗?

这几乎归结为:什么是样式中的BasedOn ="{DynamicResource ...}"的替代方案?

Nts*_*alt 4

我遇到了完全相同的问题,但是对于 ItemsContainerStyle...所以我几乎按照您所说的做了,并编写了一个 AttachedProperty ,它将允许 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是它的使用示例:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)