2016-10-17 115 views
1

我重写代码中某些控件的默认样式。在此之后,我想要禁用所有儿童(深递归)的某些控件的所有自定义样式。例如XAML:如何禁用WPF中的所有自定义样式

<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> 

我知道我可以使用样式= null,但对我来说它的坏的解决方案,因为我需要申请这一招对每种类型的控件。我如何解决我的问题?

+0

你想要一个小组的控制风格,没有一个更大的组的风格吧?如果是这种情况,你可以使用命名的资源为你的风格 – Emad

+0

我想风格的大组控件,并没有一大批控件的风格,所以命名风格不是解决方案。 – virty

回答

2

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

<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> 
+0

对于那个解决方案,我需要知道哪些样式被覆盖,并重置每个样式,那也是不好的。 – virty

相关问题