Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3437822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:06:43+00:00 2026-05-18T08:06:43+00:00

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

  • 0

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

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:06:44+00:00Added an answer on May 18, 2026 at 8:06 am

    This doesn’t work because when an element doesn’t have a style explicitly assigned, WPF finds its style by calling FindResource, using the element’s type as the key. The fact that you’ve created a style whose key is ButtonBase doesn’t matter: WPF finds the style with the key of Button or ToggleButton and uses it.

    An inheritance-based lookup method would look up the style using the element’s type, and then use the base type if no style for the element’s type was found (and keep on going until it either found a style or hit FrameworkElement). The problem is that only works if no match is found – i.e. if there’s no default style for Button, which of course there is.

    There are two things you can do. One is to do what Jens suggests, using the style’s BasedOn property to implement your own hierarchy of styles. That’s kind of annoying, though, because you have to define a style for every single type; if you don’t, the default WPF style for that type will be used.

    Another way is to use a StyleSelector that implements this lookup behavior. Like this:

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

    You can create an instance of this, give it a set of styles, and then attach it to any 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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to apply a style to all classes derived from Control. Is this
I want to apply the following Style to my Polygon : <Style xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:s=clr-namespace:System;assembly=mscorlib
I want to apply a different hyper link style to the following two things:
i want to apply masking over the textinput that need to get SSN from
I want to apply a XSL style sheet that delete duplicate nodes in my
I have the following scenario: <UserControl.Resources> <Style x:Key=NormalFontStyle> <Setter Property=Control.FontFamily Value={Binding MyFont}></Setter> </Style> <Style
I'm using CSS to style my webpage. I want to apply the following CSS
I want to apply the following style on my specific td but I dont
I have the following button style in Themes/Generic.xaml and I want it to apply
I want apply margin property for my html newsletter But Gmail ignores this CSS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.