【问题标题】:How to disable all custom styles in WPF如何禁用 WPF 中的所有自定义样式
【发布时间】:2016-10-17 15:24:24
【问题描述】:

我在代码中覆盖了某些控件的默认样式。在此之后,我想禁用某些控件的所有子项(深度递归)的所有自定义样式。例如 xaml:

    <StackPanel>
        <StackPanel.Resources>
            <Style  TargetType="Button">
                <Setter Property="Background" Value="Red"/>
            </Style>
            <Style TargetType="TextBlock">
                <Setter Property="Background" Value="Red"/>
            </Style>
        </StackPanel.Resources>
        <Button>red style here is ok</Button>
        <TextBlock> also ok</TextBlock>
        <StackPanel>
            <StackPanel.Resources>
                <!-- magic command to disable ALL custom styles, for all controls like 
                <Style TargetType = "FrameworkElement"/>   -->
            </StackPanel.Resources>
            <Button> no style plz </Button>
            <TextBlock> bad style-_- </TextBlock>
        </StackPanel>
    </StackPanel>

我知道我可以使用 style=null,但它对我来说是不好的解决方案,因为我需要对每种类型的控件应用这个技巧。我该如何解决我的问题?

【问题讨论】:

  • 您想要一小部分控件的样式,而没有样式用于较大的组,对吧?如果是这种情况,您可以为您的样式使用命名资源
  • 我想为大组控件设置样式,而大组控件没有样式,所以命名样式不是解决方案。

标签: c# wpf xaml


【解决方案1】:

您可以注入一个空白样式,该样式将优先于您的其他样式。喜欢:

<StackPanel>
    <StackPanel.Resources>
        <Style  TargetType="Button">
            <Setter Property="Background" Value="Red"/>
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="Background" Value="Red"/>
        </Style>
    </StackPanel.Resources>
    <Button>red style here is ok</Button>
    <TextBlock> also ok</TextBlock>
    <StackPanel>
        <StackPanel.Resources>
            <!-- magic command to disable ALL custom styles, for all controls -->
            <Style  TargetType="Button" />
            <Style TargetType="TextBlock" />
        </StackPanel.Resources>
        <Button> no style plz </Button>
        <TextBlock> bad style-_- </TextBlock>
    </StackPanel>
</StackPanel>

【讨论】:

  • 对于那个解决方案,我需要知道哪些样式被覆盖,并重置每种样式,这也很糟糕。
猜你喜欢
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 2011-01-11
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多