在WPF中将资源字典添加到用户控件库

15
我创建了一个用户控件类库,并在其中使用了ResourceDictionary文件。 现在,我想在WPF应用程序中使用我的用户控件,但我必须再次将ResourceDictionary文件添加到我的项目中!如果我不添加它,它会带来ResourceDictionary文件,并在MergeDictionaries块上显示错误! 我错过了什么吗?
    <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>

增加用户控制:

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

30

试一下这个:

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

3
这对我没有用,让我非常疯狂,因为其他地方都说这是有效的 - 但它不起作用! - ThisHandleNotInUse
4
需要一把字典的钥匙。 - OliverAssad
7
请注意,所有其他资源都需要在 <ResourceDictionary> 标记内,否则会引发“多次设置资源”的错误。 - Hexo
就像@Hexo所说的那样!但是错误不是“设置多次”,而是“这需要一个键”,这完全是误导性的。但是,解决方案是将所有其他资源放在字典中。 - ecth

4

仅澄清@Willem的答案:在合并字典后有额外资源时,可能会出现错误“x:key属性是必需的”。在这种情况下,这些资源必须位于资源字典

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

    <ResourceDictionary>
 </UserControl.Resources>

 <Grid />

1

回应@ThisHandleNotInUse和@OliverAssad在已接受的答案中的评论。

如果出现“x:Key属性必需”错误,则应将ResourceDictionary标记修改如下:

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

@MichaelBrown 你为什么认为这是错误的?至少在2016年,使用当时存在的版本,这是可以工作的。确切地说,我的答案试图解决已接受答案中所需的x:Key属性错误,没有其他意思。 - Athafoud
UserControl.Resources 无法接受一个无效的 Key。ResourceDictionary 部分是正确的。 - Michael Brown

0

另一种方法是在应用程序级别添加资源

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接