这不起作用,因为当元素没有显式分配样式时,WPF 通过调用FindResource 来查找其样式,使用元素的类型作为键。您创建了键为 ButtonBase 的样式这一事实并不重要:WPF 会找到键为 Button 或 ToggleButton 的样式并使用它。
基于继承的查找方法会使用元素的类型来查找样式,如果没有找到该元素类型的样式,则使用基本类型(并继续进行,直到找到样式或点击FrameworkElement )。问题是只有在没有找到匹配项时才有效 - 即如果没有 Button 的默认样式,当然有。
您可以做两件事。一种是按照 Jens 的建议,使用样式的 BasedOn 属性来实现您自己的样式层次结构。不过,这有点烦人,因为您必须为每种类型定义一个样式;如果不这样做,将使用该类型的默认 WPF 样式。
另一种方法是使用实现此查找行为的StyleSelector。像这样:
public class InheritanceStyleSelector : StyleSelector
{
public InheritanceStyleSelector()
{
Styles = new Dictionary<object, Style>();
}
public override Style SelectStyle(object item, DependencyObject container)
{
Type t = item.GetType();
while(true)
{
if (Styles.ContainsKey(t))
{
return Styles[t];
}
if (t == typeof(FrameworkElement) || t == typeof(object))
{
return null;
}
t = t.BaseType;
}
}
public Dictionary<object, Style> Styles { get; set; }
}
您可以为此创建一个实例,为其设置一组样式,然后将其附加到任何ItemsControl:
<Window x:Class="StyleSelectorDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:StyleSelectorDemo="clr-namespace:StyleSelectorDemo" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<StyleSelectorDemo:InheritanceStyleSelector x:Key="Selector">
<StyleSelectorDemo:InheritanceStyleSelector.Styles>
<Style x:Key="{x:Type ButtonBase}">
<Setter Property="ButtonBase.Background"
Value="Red" />
</Style>
<Style x:Key="{x:Type ToggleButton}">
<Setter Property="ToggleButton.Background"
Value="Yellow" />
</Style>
</StyleSelectorDemo:InheritanceStyleSelector.Styles>
</StyleSelectorDemo:InheritanceStyleSelector>
</Window.Resources>
<Grid>
<ItemsControl ItemContainerStyleSelector="{StaticResource Selector}">
<Button>This is a regular Button</Button>
<ToggleButton>This is a ToggleButton.</ToggleButton>
<TextBox>This uses WPF's default style.</TextBox>
</ItemsControl>
</Grid>
</Window>