valueconverter在资源词典中的未知

时间:2012-01-02 10:10:14

标签: wpf resourcedictionary valueconverter

我在项目中使用了两次列表框样式,所以我想在资源字典中使用它。此列表框样式中包含两个值转换器,因此我在同一资源文件中实例化转换器。在运行时虽然它说“未知类型无法声明”,尽管在mainwindow.xaml文件中使用它们时,相同的转换器声明仍然有效。

有人有想法吗?

1 个答案:

答案 0 :(得分:4)

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

如果您使用其他ResourceDictionaries,有两种方法可以在所有情况下使用ResourceDictionary.MergedDictionaries,如下所示:

<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