15

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>
Nitesh
  • 7,261
  • 3
  • 30
  • 25

4 Answers4

30

Give this a try:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
Willem
  • 9,166
  • 17
  • 68
  • 92
  • 3
    This doesn't work for me and it's driving me absolutely crazy because everywhere else they say this works - but it doesn't! – ThisHandleNotInUse Mar 13 '15 at 03:10
  • 4
    This requires also a key for the dictionaty – OliverAssad Oct 13 '15 at 14:50
  • 7
    Note that all other resources need to be within the tag or it will cause a "resources set more than once" error – Hexo Dec 20 '17 at 11:52
  • What @Hexo says! But the error is not "set more than once" but "this requires a key" which is totally misleading. But yes, the solution is to put all other resources inside the dictionary. – ecth Sep 29 '21 at 13:18
4

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 />
Slesa
  • 245
  • 2
  • 11
1

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>
Community
  • 1
  • 1
Athafoud
  • 2,898
  • 3
  • 40
  • 58
  • @MichaelBrown Why do you think this is wrong? At least back in 2016, with the version existing that time, this was working. To be exact my answer tries to solve the x:Key attribute required error of the accepted answer, nothing more. – Athafoud Jun 25 '20 at 06:21
  • `UserControl.Resources` can't accept a Key, that's invalid. The ResourceDictionary part is correct – Michael Brown Jun 25 '20 at 18:31
0

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>
Scott
  • 1