Adding resource dictionaries to a usercontrol library in wpf
Asked Answered
L

4

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>
Legault answered 23/7, 2013 at 10:15 Comment(0)
G
30

Give this a try:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
Gudgeon answered 23/7, 2013 at 10:20 Comment(5)
This doesn't work for me and it's driving me absolutely crazy because everywhere else they say this works - but it doesn't!Viscera
This requires also a key for the dictionatyInhalant
Note that all other resources need to be within the <ResourceDictionary> tag or it will cause a "resources set more than once" errorBiweekly
What @Biweekly 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.Arioso
If I use it in ItemTemplate, does it make new instance of those imports for every item in the list?Vanitavanity
P
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 />
Publicly answered 21/8, 2020 at 9:23 Comment(0)
S
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>
Spherule answered 12/9, 2016 at 12:41 Comment(2)
@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.Spherule
UserControl.Resources can't accept a Key, that's invalid. The ResourceDictionary part is correctWanderlust
K
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>
Konrad answered 30/1, 2020 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.