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

4 回答 4

29

试试这个:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
于 2013-07-23T10:20:43.260 回答
4

只是为了澄清@Willem的答案:在合并字典后有额外的资源时,可能会出现错误“x:key attribute is required”。在这种情况下,这些资源必须资源字典中:

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

    <ResourceDictionary>
 </UserControl.Resources>

 <Grid />
于 2020-08-21T09:23:23.007 回答
1

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

如果出现x:Key attribute required错误,ResourceDictionary应将标签修改如下:

<UserControl.Resources x:Key="myKey">
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
于 2016-09-12T12:41:02.613 回答
0

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

<Application.Resources>
     <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ProjectStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
于 2020-01-30T23:24:16.950 回答