How to apply style to a class and its descendants?

I want to apply the following style to all controls that are inferred from ButtonBase

<Style
    TargetType="{x:Type ButtonBase}">
    <Setter
        Property="Cursor"
        Value="Hand" />
</Style>

But it works only for this class, and not for its descendants. How to achieve what I intend?

+5
source share
2 answers

This does not work because when an element does not have an explicitly assigned style, WPF finds its style by calling FindResourceusing the element type as a key. The fact that you created the style with the key ButtonBasedoes not matter: WPF finds the style with the key Buttonor ToggleButtonand uses it.

, , , , ( , FrameworkElement). , , - , Button, , , .

, . - , , style 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 default style.</TextBox>
        </ItemsControl>
    </Grid>
</Window>
+4

.

, "" , .

<Style x:Key="ButtonBaseStyle" TargetType="{x:Type ButtonBase}">
    <!-- Style stuff -->
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonBaseStyle}">
    <!-- Additional style stuff for button only -->
</Style>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource ButtonBaseStyle}">
    <!-- Additional style stuff for toggle button only -->
</Style>
<!-- more ButtonBase descendants here  -->
+1

All Articles