2015-12-03 50 views
0

我在一個名爲ItemsTemplate的單獨項目中有一個資源字典。來自不同項目的資源字典拋出異常

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style TargetType="{x:Type TextBox}" 
      x:Key="TextBoxesTemplate" 
      > 
     <Setter Property="OverridesDefaultStyle" 
       Value="True"/> 
     <Setter Property="Margin" 
       Value="5"/> 
     <Setter Property="BorderThickness" 
       Value="1"></Setter> 
    </Style> 
</ResourceDictionary> 

我想在許多不同的項目中使用它。而我拋出一個異常,當我運行它,說: 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.'

所以這裏是我執行什麼我曾嘗試:

<ResourceDictionary Source="/ItemTemplates;component/Style.xaml"></ResourceDictionary> 

<TextBox Style="{StaticResource TextBoxesTemplate}" 
     Grid.Row="0" 
     Grid.Column="2" 
     Text="{Binding SelectedModel.Code}"/> 

我試圖改變

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/ItemTemplates;component/Style.xaml"> 
</ResourceDictionary.MergedDictionaries> 

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/ItemTemplates;component/Style.xaml"> 
</ResourceDictionary.MergedDictionaries> 

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="pack:application,,,/ItemTemplates;component/Style.xaml"> 
</ResourceDictionary.MergedDictionaries> 

注:這是.NET 4.5.2

編輯:建立用這個。但仍然得到例外

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/ItemTemplates;component/Style.xaml" 
           x:Name="SDictionary" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 
+0

我猜你正在設置你的字典內的資源標籤?像然後你的合併字典 adminSoftDK

+0

是的,我使用的是。資源 – JamTay317

回答

0

請參考鏈接:只要當前的項目具有單獨的項目參考https://msdn.microsoft.com/en-us/library/aa970069(VS.85).aspx

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/ItemTemplates;component/Style.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

請,因爲在你的問題再一次參考和Source="/ItemTemplatesSource="/ItemsTemplate檢查,你寫的是:「我有一個單獨的項目資源字典稱爲ItemsTemplate。」

希望可以幫到你!

+0

它是ItemTemplates – JamTay317

+0

沒關係,我已經引用了你建議的頁面,但它仍然沒有工作。我不用「.MergedDictionaries」構建是否需要? – JamTay317

+0

如果您只使用一個ResourceDictionary –