开发者

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

开发者 https://www.devze.com 2023-02-12 07:26 出处:网络
I usually define style and control template in App.xml undertab. So, while designing the UI, I can see the UI with the style applied in design开发者_开发技巧 view in Visual Studio 2008 with .NET3.5.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消