14
votes

I have created a user control class library and I used a ResourceDictionary file in it. Now, I want to use my usercontrol in a WPF application, but I have to add ResourceDictionary file again in my projet! If I don't add it, it brings the ResourceDictionary file, and show an error on MergeDictionaries block! Am I missing something!?

Resource dictionary is:

    <ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}">
        <Rectangle Fill="Transparent" Cursor="Hand"/>
    </ControlTemplate>

    <Style x:Key="ItemStyle" TargetType="ContentControl">
        <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Canvas}},Path=ActualWidth}"/>
        <Setter Property="MinHeight" Value="60"/>
        <Setter Property="Height" Value="60"/>
        <Setter Property="Content" Value="MyTextBox"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                        <s:MoveThumb Template="{DynamicResource MoveThumbTemplate}"/>
                        <ContentPresenter Name="MainControl" Content="{TemplateBinding ContentControl.Content}"
                                          Margin="5,0,10,0"/>
                        <Grid Opacity="0" Margin="-3">
                            <s:ResizeThumb Height="3" Cursor="SizeNS" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
                            <s:ResizeThumb Height="3" Cursor="SizeNS" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>    
</ResourceDictionary>

adding to user control:

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

4 Answers

27
votes

Give this a try:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
3
votes

Just to clearify the answer of @Willem: when having additional resources after the merged dictionary, there may appear the error "x:key attribute is required". In this case, those resources have to be inside the resource dictionary:

 <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary source="/assembly;component/resource.xaml" />
        </ResourceDictionary.MergedDictionaries>
      
        <Style TargetType="TextBlock" BasedOn="{StaticResource SetupTextBlockStyle}" />

    <ResourceDictionary>
 </UserControl.Resources>

 <Grid />
1
votes

In response to @ThisHandleNotInUse and @OliverAssad comments in the accepted answer.

In case of x:Key attribute required error, the ResourceDictionary tag should be modified as follows:

<UserControl.Resources x:Key="myKey">
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
0
votes

Another way is to add the resource at an application level

<Application.Resources>
     <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ProjectStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>