4

I have run into a little(well big) problem in an application I am working on.

I am working on a module for an application for my company. The application is a WinForm application, but I have been working on a WPF application(not really an application as you will see) that is going to be hosted in this WinForm application when it is complete.

To do this, I am using the WinForm element host, and I have created a "shell" user control, and then other user control windows inside of that shell user control. So it appears as a WPF application, and only uses the WinForm application as its startup project since the WPF application is really only a collection of WPF Controls.

The problem I have ran into is that since I have not created an actual "WPF Application", there is no App.xaml. This has disabled me from using Shared Resources the way I desire, especially XAML shared resources.

Is there a way I can still treat my collection of WPF User Controls as a WPF Application, and somehow use a App.xaml file for my resources. If not, what are my options for using shared resources in my application.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
TheJediCowboy
  • 8,924
  • 28
  • 136
  • 208

1 Answers1

1

Add a ResourceDictionary (xaml) file to your project (assuming it's a class library - WPF custom control library), merge it on top of the Generic.xaml, then you will be able to refer to it and your StaticResource will work.

You can also include the resource in the Generic.xaml (or whatever xaml file it is) file itself.

Here is how your current dictionary should look like:

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"   
  xmlns:local="clr-namespace:WpfCustomControlLibrary1">

  <sys:String x:Key="myString">sdfasdf</sys:String>

  <Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Text" Value="{StaticResource myString}"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:CustomControl1}">
          <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
            <TextBlock Text="{TemplateBinding Text}"/>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

I initialized a design time instance of the above control in VS2010 and it showed the text (Text is a string DP property I manually added to the CustomControl1), which means it reads the myString resource.

You can find some more specific info here and here.

Community
  • 1
  • 1
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632