2012-01-02 118 views
0

我在项目中两次使用了列表框样式,所以我想在资源字典中使用它。这个列表框样式有两个值转换器,所以我在相同的资源文件中实例化了转换器。尽管它在运行时声明“未知类型不能声明”,尽管在mainwindow.xaml文件中使用它们时工作原理相同。valueconverter在资源字典中的未知

任何人有想法?

+1

你可以发布一些代码(listboxstyle与转换器的声明)我看不出有什么问题,这样做 – blindmeis 2012-01-02 12:15:07

回答

4

我有相同的问题,直到我将转换器移动到资源部分下的App.xaml文件。这是我的示例App.xaml文件,我刚创建了一个名为TextConverter的转换器作为示例。

有两种方法,如果你使用的是你必须使用在所有情况下ResourceDictionary.MergedDictionaries其他ResourceDictionaries要做到这一点,如下所示:

<Application x:Class="WPFFeatureSample_Application.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:converters="clr-namespace:WPFFeatureSample_Application.Converters" 
     StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary> 
       <converters:TextConverter x:Key="TextConverter1"></converters:TextConverter> 
      </ResourceDictionary> 
      <ResourceDictionary Source="Resources/ControlDictionary.xaml"></ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

如果没有其他资源档案,这将是这样的:

<Application x:Class="WPFFeatureSample_Application.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:converters="clr-namespace:WPFFeatureSample_Application.Converters" 
     StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <ResourceDictionary> 
     <converters:TextConverter x:Key="TextConverter1"></converters:TextConverter> 
    </ResourceDictionary> 
</Application.Resources> 

一个有趣的参考使用一般的转换器和资源字典的时候是这样的: http://www.dotnetdude.com/2011/01/23/ResourceDictionariesAreMakingMyHairHurt.aspx

+0

这帮助我,谢谢! – raphi011 2012-01-09 10:37:05

+0

这对我也有帮助。它不需要在App.xaml字典中。关键是将转换器封装在标签中,并将其放在Resources或MergedDictionaries中。 – Eternal21 2014-06-20 18:33:21