In WPF, how do I apply a style to a UserControl in a design view?

I usually define a style and control pattern in App.xml under a tab. Thus, when developing the user interface, I can see the user interface with the style used in the design view in Visual Studio 2008 with .NET3.5.

However, in another case, sometimes I only do the UserControl project, and in this case there is no App.xml, so the user interface is displayed as an appearance in the design view. It is hard to know the actual search at runtime.

Is there a way to apply a style to a UserControl? If there is a way to share the same style and template between the application project and the UserControl project, this will be perfect!

Please let me know if there is a solution.

+8
c # styles wpf user-controls controltemplate
source share
1 answer

You can save your styles in a separate ResourceDictionary in your management project. Then you need to Combine the dictionaries in the Resources block at the top of each user control:

<UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="MyControlLibraryStyles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> 

The problem with this approach is that you are losing any benefit that App-level styles would give you (e.g., styles differently for different applications), because now all styles are defined by the underlying control.

+4
source share

All Articles