2016-02-11 39 views
0

为什么我的Radiobutton看起来不像一个togglebutton? 看看代码WPF样式BasedOn没有按预期工作

<Style x:Key="ButtonBaseStyle" TargetType="{x:Type ButtonBase}"> 
<Setter Property="Height" Value="100" /> 
</Style> 
<Style BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type Button}" /> 
<Style BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type ToggleButton}" /> 
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" /> 


<StackPanel> 
    <Button>Button</Button> 
    <ToggleButton>Toggle</ToggleButton> 
    <RadioButton>Radio</RadioButton> 
</StackPanel> 

如果我删除在ButtonBase风格它的工作原理

+0

你是什么意思“看起来不像一个切换按钮”呢?删除按钮库样式后,什么“起作用”,但之前没有? –

回答

1

RadioButton的外观并不像ToggleButton原因,现在你已经在改变ButtonBase->ToggleButton->RadioButton(顺序式的层次结构哪些样式和模板在WPF派生类中被覆盖)。

你的风格新的层次结构给出了最优先的ButtonBase风格。所以你有一个新的ToggleButton风格是从ButtonBase派生的,然后用这个风格覆盖RadioButton风格。因此,没有人可以确定哪些属性被覆盖,哪些属性是最后一组属性。

为了理解这一变化的XAML如下:

<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type ToggleButton}" /> 
    <Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" /> 

,现在看到你RadioButton的外观&感觉。

preview

我不澄清究竟是什么发生了,为什么你ToggleButton不会受到影响这么大。 但是,正如我所说的,它只是在用户界面上呈现之前最后为控件构建的样式层次结构。

+1

我想念红的问题,我很着急/ AM,您有适合的好问题的答案,我将深入研究一下,当我回家,+1 – Usama

+0

@SamTheDev没有概率。我相信你会提供更好的解释,因为我没有提供非常明确的答案。 –