Derived classes and topic inheritance in WPF?

I am creating a WPF application, and I got a bunch of controls from the standard WPF control types - text blocks, buttons, etc. I tried adding a resource dictionary to app.xaml to install the theme, but my user interface controls do not seem to respect it. (For example, the standard buttons are great for the Aero theme, but the myButton obtained from the Button still doesn't work.) Is there a way I can set the theme for my derived controls to the same as for the basic controls?

EDIT: I should note that these custom controls are created at runtime, so I cannot directly control their properties through XAML. I can change certain properties, such as the background color, using Setter in the application resource dictionary, but have not found a way to set the theme using this technique.

+4
source share
1 answer

If you have this style in Dictionary1.xaml Resource Dictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"> <Setter Property="Width" Value="75" /> <Setter Property="Height" Value="23" /> </Style> </ResourceDictionary> 

Then you can install it on any button with this code for

 Uri resourceLocater = new Uri("/YourAssemblyName;component/Dictionary1.xaml", System.UriKind.Relative); ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater); Style myButtonStyle = resourceDictionary["MyButtonStyle"] as Style; Button button = new Button(); button.Style = myButtonStyle; 
+1
source

All Articles