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

  • Home
  • SEARCH
  • 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 153885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:52:50+00:00 2026-05-11T09:52:50+00:00

Suppose that you are developing a custom control in WPF that contains internally some

  • 0

Suppose that you are developing a custom control in WPF that contains internally some other basic controls. To keep it simple suppose that it contains 2 buttons.

Now you want to use this custom control in your app, but you want to restyle it a bit.

CASE 1

If, in the custom control definition, both buttons have the same style (wpf default) and you want to restyle both, it should be easy:

<mc:MyControl>    <mc:MyControl.Resources>       <Style x:Key={x:Type Button}, TargetType={x:Type Button}>          <!-- Insert the new style here -->       </Style>    </mc:MyControl.Resources> <mc:MyControl> 

CASE 2

If, in the custom control definition, both buttons have the same style (wpf default) but you want to restyle them with two different styles, what’s the best way to solve it?

CASE 3

If, in the custom control definition, both buttons have the same style, that refer to a Style defined inside the custom control, and you want to restyle them, what’s the best way to solve it?

Thank you in advance for all help

  • 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. 2026-05-11T09:52:50+00:00Added an answer on May 11, 2026 at 9:52 am

    You could define 2 different Style properties in your custom control and bind them to the Style property of the individual buttons, thus allowing clients to set the style for the two controls independently of each other.

    XAML file:

    <UserControl x:Class='MyNamespace.MyControl'   xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'   xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'   Name='MyControl'>   <StackPanel>     <Button Name='FirstButton'             Style={Binding ElementName=MyControl, Path=FirstButtonStyle}             Content='First Button' />     <Button Name='SecondButton'             Style={Binding ElementName=MyControl, Path=SecondButtonStyle}             Content='Second Button' />   </StackPanel> </UserControl> 

    Code-behind file:

    using System; using System.Windows; using System.Windows.Controls;  namespace MyNamespace {   [StyleTypedProperty(   Property = 'FirstButtonStyle',   StyleTargetType = typeof(Button))]   [StyleTypedProperty(   Property = 'SecondButtonStyle',   StyleTargetType = typeof(Button))]   public partial class MyControl : UserControl   {     public static readonly DependencyProperty FirstButtonStyleProperty =       DependencyProperty.Register(         'FirstButtonStyle',         typeof(Style),         typeof(MyControl)       );      public Style FirstButtonStyle     {                 get { return (Style)GetValue(FirstButtonStyleProperty); }       set { SetValue(FirstButtonStyleProperty, value); }     }       public static readonly DependencyProperty SecondButtonStyleProperty =       DependencyProperty.Register(         'SecondButtonStyle',         typeof(Style),         typeof(MyControl)       );      public Style SecondButtonStyle     {                 get { return (Style)GetValue(SecondButtonStyleProperty); }       set { SetValue(SecondButtonStyleProperty, value); }     }   } } 

    It is a good idea to implement these 2 properties as dependency properties, since that will make them conform to the other Style properties in standard WPF controls.

    Now you can set the style for the buttons like you would in any WPF control:

    <Window    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'   xmlns:local='clr-namespace:MyNamespace'   Title='MyControl Sample'    Height='300'    Width='300'>   <Window.Resources>     <Style x:Key='GreenButton' TargetType='{x:Type Button}'>       <Setter Property='Background' Value='Green' />     </Style>     <Style x:Key='RedButton' TargetType='{x:Type Button}'>       <Setter Property='Background' Value='Red' />     </Style>   </Windows.Resources>   <StackPanel>     <local:MyControl FirstButtonStyle='{StaticResource GreenButton}'                      SecondButtonStyle='{StaticResource RedButton}' />   </StackPanel> </Window> 

    Alternatively you could have the 2 buttons share the same Style by exposing a single property in your custom control which you bind to the Style property of both internal controls.

    UPDATE

    You don’t have to define the FirstButtonStyle and SecondButtonStyle properties as dependency properties. The important thing is that the internal bindings to the buttons’ Style properties are updated whenever their value changes. You can accomplish this by implementing the INotifyPropertyChanged interface in your user control and raise the OnPropertyChanged event in the property setters.

    You could also assign a ‘default style’ to the 2 properties in the user control’s constructor. Here is an example:

    using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls;  namespace MyNamespace {   [StyleTypedProperty(   Property = 'FirstButtonStyle',   StyleTargetType = typeof(Button))]   [StyleTypedProperty(   Property = 'SecondButtonStyle',   StyleTargetType = typeof(Button))]   public partial class MyControl : UserControl, INotifyPropertyChanged   {     private Style firstButtonStyle;     private Style secondButtonStyle;      public MyControl()     {       Style defaultStyle = new Style();       // assign property setters to customize the style        this.FirstButtonStyle = defaultStyle;       this.SecondButtonStyle = defaultStyle;      }      public Style FirstButtonStyle     {                 get { return firstButtonStyle; }       set       {          firstButtonStyle = value;          OnPropertyChanged('FirstButtonStyle');       }     }      public Style SecondButtonStyle     {                 get { return secondButtonStyle; }       set       {          secondButtonStyle = value;          OnPropertyChanged('SecondButtonStyle');       }     }      public event PropertyChangedEventHandler PropertyChanged;      protected virtual void OnPropertyChanged(string propertyName)     {       if (PropertyChanged != null)       {         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));       }     }   } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The context of the event handler is set to the… May 12, 2026 at 12:01 am
  • Editorial Team
    Editorial Team added an answer DatePicker has a property Format that can be set to… May 12, 2026 at 12:01 am
  • Editorial Team
    Editorial Team added an answer I figured it out finally and it had nothing to… May 12, 2026 at 12:01 am

Related Questions

I intend to develop a system that is entirely based on modules. The system
Suppose you are developing a library with classes to be exported through a DLL
Suppose you're developing a software product that has periodic releases. What are the best
I am the lone software engineer on a team that develops physics models (approx

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.