Wpf XAML样式中的条件基

Wpf XAML样式中的条件基,wpf,xaml,Wpf,Xaml,鉴于: ControlTemplate具有样式WidgetControlTemplate。我希望(BasedOn)将WidgetControlTemplate样式有条件地基于ThirdLevelGroupBoxStyle或SecondLevelGroupBoxStyle,以避免XAML重复。有没有办法做到这一点 如果我不能这样做,我必须复制SecondLevelGroupBoxStyle的定义 有没有办法做到这一点 不,不是在XAML中。在设计时必须知道要基于样式的基础样式 如果我不能这样做

鉴于:


ControlTemplate
具有样式
WidgetControlTemplate
。我希望(
BasedOn
)将
WidgetControlTemplate
样式有条件地基于
ThirdLevelGroupBoxStyle
SecondLevelGroupBoxStyle
,以避免XAML重复。有没有办法做到这一点

如果我不能这样做,我必须复制
SecondLevelGroupBoxStyle
的定义

有没有办法做到这一点

不,不是在XAML中。在设计时必须知道要基于样式的基础样式

如果我不能这样做,我必须复制SecondLevelGroupBoxStyle的定义

由于有两种基于MetroGroupBox的不同样式,因此基于这两种样式之一的每个样式都将始终是单独的样式,因为不能基于多个样式创建单个样式

您可以使用触发器将GroupBox的背景属性设置为AccentColorBrush3或AccentColorBrush2,而不是尝试从其他样式继承此属性。看起来您已经在使用WidgetControlTemplateStyle进行此操作了。是的,如果ThirdLevelGroupBoxStyle/SecondLevelGroupBoxStyle设置了多个属性,则还必须在WidgetControlTemplateStyle中设置所有这些属性


恐怕没有办法解决这个问题,除非你将两种风格合并为一种风格,或者以某种方式通过编程来定义它们。

谢谢,至少我知道没有好的方法可以做到这一点。是的,它不仅仅是一个财产;(
<Style x:Key="ThirdLevelGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource MetroGroupBox}">
    <Setter Property="Background" Value="{DynamicResource AccentColorBrush3}" />
</Style>

<Style x:Key="SecondLevelGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource MetroGroupBox}">
    <Setter Property="Background" Value="{DynamicResource AccentColorBrush2}" />
</Style>

<Style TargetType="GroupBox" x:Key="WidgetControlTemplateStyle" BasedOn="{StaticResource ThirdLevelGroupBoxStyle}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding CanExecuteClickCommand}" Value="True">
            <!-- TODO: SecondLevelGroupBoxStyle -->
            <Setter Property="Background" Value="{DynamicResource AccentColorBrush2}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<ControlTemplate TargetType="ContentControl" x:Key="WidgetControlTemplate">
    <GroupBox ... Style="{StaticResource WidgetControlTemplateStyle}">
        <ContentPresenter />
    </GroupBox>
</ControlTemplate>