简体   繁体   中英

In WPF, How to apply style to UserControl in design view?

I usually define style and control template in App.xml under tab. So, while designing the UI, I can see the UI with the style applied in design view in Visual Studio 2008 with .NET3.5.

However, in another case, sometimes I make UserControl only project and in that case, there's no App.xml so the UI is appeared as default appearance in design view. It's hard to know actual looking in runtime.

Is there any way to apply style to UserControl, too? If there's a way to share same style and template between application project and UserControl project, that would be perfect!

Please let me know if there's a solution.

You can keep your styles in a separate ResourceDictionary in your control project. Then you need to Merge Dictionaries in the Resources block at the top of every user control:

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

The problem with this approach is of course that you lose any benefit that App-level styles would give you (like styling differently for different applications) because now the styles are all determined by the underlying control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM