Assembly-wide / root-level styles in WPF class library

后端 未结 7 2001
夕颜
夕颜 2021-01-30 06:28

I have a C# (2008/.NET 3.5) class library assembly that supports WPF (based on this article).
I\'ve created several windows, and am now attempting to create a common style

7条回答
  •  醉酒成梦
    2021-01-30 07:14

    So after spending a lot of time I finally figured this out. Here's how:

    1. In WPF controls library, add a new folder named themes.
    2. Inside the themes folder, add a resource dictionary named generic.xaml.
    3. Inside generic.xaml, add your resource using the following syntax:

      
      
    4. In your control, use the following syntax to access this resource:

      Background="{StaticResource {ComponentResourceKey {x:Type local:UserControl1}, MyEllipseBrush}}"
      

    Things to note:

    1. Step 1 and 2 are normally automatically done for you by Visual Studio when you create a new WPF Controls Library.
    2. I do not fully understand the purpose of ComponentResourceKey's first parameter, but it is required. Use the name of the control that will consume this resource.
    3. Visual Studio's designer was not be able to locate the resource in my case. It could be a cache issue, I'm not sure. At runtime however it works neatly.
    4. You can read more details about this syntax in this MSDN article.

    Hope this makes some lives easier.

提交回复
热议问题