在wpf中将资源字典添加到usercontrol库

时间:2013-07-23 10:15:18

标签: wpf user-controls

我创建了一个用户控件类库,并在其中使用了ResourceDictionary文件。 现在,我想在WPF应用程序中使用我的usercontrol,但我必须在我的项目中再次添加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 个答案:

答案 0 :(得分:21)

尝试一下:

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

答案 1 :(得分:2)

  

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

如果出现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>

答案 2 :(得分:2)

只是为了澄清@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 />

答案 3 :(得分:0)

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

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