user2205930
user2205930

Reputation: 1086

FindResource not working in Custom Control DLL

While I am still learning WPF I have yet run into another problem. I have a DLL which is of type Custom Control. I've implemented my base control and I have several controls which derive from this base; the base control is never used. The problem is whenever I call FindResource or TryFindResource it always fails. I have a separate dictionary that I merged in my Themes/Generic.xaml file:

Gernieric.xaml

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/DllProject;component/Themes/NewResource.xaml" />
    <!-- I've tried several other ways for the Source format, i.e. pack:... -->
</ResourceDictionary.MergedDictionaries>

Now I am trying to set the Style of my derived controls in the derived controls constructor without the control being on a visible canvas or panel at the time. I also want to export a VisualBrush of the control as a BitmapSource. All of the code was working when I put my NewResource.xaml in the EXE project (where it doesn't belong). I've read articles on adding a dummy tag to the resource dictionary as well as put all of my code in the Generic.xaml file. Like I said all of this works fin if move the xaml file to the main EXE. Its as if the DLL isn't even loading the xaml file or even aware there is anything declared in it until the control is on a visible window.

I have a style for my base control (NewResource.xaml):

<Style TargetType="{x:Type local:MyDerivedControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyDerivedControl}">
                <Grid>
                    <Path x:Name="MyPath" Style="{TemplateBinding DepProp}" />
                    <TextBlock x:Name="Text" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{TemplateBinding Text}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

After doing a lot of research on the internet it seems WPF dropped the ball on DLLs and resources.

Upvotes: 1

Views: 1347

Answers (1)

user2205930
user2205930

Reputation: 1086

I found the answer that worked. Basically change the x:Key to contain a ComponentResourceKey. A full description can be viewed here that is straight forward to understand.

Upvotes: 2

Related Questions