Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF样式中的继承反转_C#_Wpf - Fatal编程技术网

C# WPF样式中的继承反转

C# WPF样式中的继承反转,c#,wpf,C#,Wpf,我有一个UserControl,其中包含一个按钮: <Button Content="Button"/> 还有一种风格: <Style TargetType="Button"> <Setter Property="Background" Value="Blue"/> </Style> 父窗口(或另一个用户控件)可以设置另一个更通用的样式: <Style TargetType="Button"> <Set

我有一个UserControl,其中包含一个按钮:

<Button Content="Button"/>

还有一种风格:

<Style TargetType="Button">
    <Setter Property="Background" Value="Blue"/>
</Style>

父窗口(或另一个用户控件)可以设置另一个更通用的样式:

<Style TargetType="Button">
    <Setter Property="Background" Value="Red"/>
</Style>

结果是(显而易见的)父按钮将具有更一般的样式(红色),而我的用户控件将具有更特定的样式(蓝色)的按钮


我想知道如何反转这种行为,以实现像在自定义用户控件中设置默认样式这样的功能,如果需要的话,可以在父控件或窗口中重写它

关键是,默认样式首先在自定义用户控件中定义,并由其父控件自动覆盖。这就是我所说的倒置


解决方案maight的想象示例如下所示:

<Style TargetType="Button" StylePriority="Default">
    <Setter Property="Background" Value="Blue"/>
</Style>


StylePriority
可能表示,如果没有为该按钮定义其他样式,则应将默认样式应用于该按钮。

UserControl
中为按钮颜色创建一个依赖项属性,然后绑定到该属性。可以为该属性指定默认值蓝色

public static readonly DependencyProperty ButtonColorProperty = 
    DependencyProperty.Register("ButtonColor", typeof(Color), typeof(MyUserControl),
    new PropertyMetadata(Colors.Blue));

public Color State
{
    get { return (Color)this.GetValue(ButtonColorProperty); }
    set { this.SetValue(ButtonColorProperty, value); } 
}

<UserControl ...
             x:Name="root">
    <Button Content="Button" Background="{Binding ElementName=root, Path=ButtonColor}" />
</UserControl>

您可以使用动态资源

用户控件:

<UserControl x:Class="Example.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Example">
    <UserControl.Resources>
        <Style TargetType="local:UserControl1">
            <Style.Resources>
                <Style TargetType="Button" x:Key="UserControl1.DefaultButtonStyle">
                    <Setter Property="Background" Value="Red"/>
                </Style>
            </Style.Resources>
        </Style>
    </UserControl.Resources>

    <Button Content="UserControlButton" Style="{DynamicResource UserControl1.DefaultButtonStyle}"/>
</UserControl>

还有一扇窗户:

<Window x:Class="Example.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Example">

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="Blue" />
        </Style>
    </Window.Resources>

    <StackPanel>
        <local:UserControl1 >
            <local:UserControl1.Resources>
                <Style x:Key="UserControl1.DefaultButtonStyle" TargetType="Button"
                    BasedOn="{StaticResource {x:Type Button}}">
                    <Setter Property="FontSize" Value="40" />
                </Style>
            </local:UserControl1.Resources>
        </local:UserControl1>
        <Button Content="WindowButton" />
    </StackPanel>
</Window>


如果在窗口中删除控件的样式,将应用默认的用户控件按钮样式。

如果要在父控件中使用默认样式,为什么还要在
usercontrol
?@Bolu:编辑以更好地理解中定义另一个样式呢。我想在用户控件中定义默认样式,但我希望能够在父窗口中覆盖它。假设:自定义控件中定义的默认值为蓝色按钮。在父窗口中覆盖它以获得红色按钮。我不是100%确定,但我不认为有一种简单的方法可以做你不想做的事情,就像反转VisualTree:)
<Window x:Class="Example.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Example">

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="Blue" />
        </Style>
    </Window.Resources>

    <StackPanel>
        <local:UserControl1 >
            <local:UserControl1.Resources>
                <Style x:Key="UserControl1.DefaultButtonStyle" TargetType="Button"
                    BasedOn="{StaticResource {x:Type Button}}">
                    <Setter Property="FontSize" Value="40" />
                </Style>
            </local:UserControl1.Resources>
        </local:UserControl1>
        <Button Content="WindowButton" />
    </StackPanel>
</Window>