2015-12-03 31 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 –