2013-07-23 152 views
10

我創建了一個用戶控件類庫,並在其中使用了ResourceDictionary文件。 現在,我想在WPF應用程序中使用我的usercontrol,但是我必須在我的projet中再次添加ResourceDictionary文件!如果我不添加它,它會帶來ResourceDictionary文件,並在MergeDictionaries塊上顯示錯誤! 我錯過了什麼!?將資源字典添加到wpf中的用戶控件庫

資源字典是:

<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> 

回答

19

這給一試:

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

這不工作對我來說,它的駕駛我絕對是瘋了,因爲在其他地方,他們說這個工作 - 但它不! – ThisHandleNotInUse

+2

這還需要一個詞典密鑰 – OliverAssad

+0

請注意,所有其他資源需要位於標記內,否則會導致「資源集多次」錯誤 – SlapY

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> 
相關問題