Wpf 值转换器';在资源字典中未知

Wpf 值转换器';在资源字典中未知,wpf,resourcedictionary,valueconverter,Wpf,Resourcedictionary,Valueconverter,我在项目中使用了两次列表框样式,所以我想在资源字典中使用它。这个列表框样式中有两个值转换器,所以我在同一个资源文件中实例化了这些转换器。虽然在mainwindow.xaml文件中使用相同的转换器声明时也可以使用,但在运行时它表示“无法声明未知类型” 有人有想法吗?我也有同样的问题,直到我将转换器移动到参考资料部分下的App.xaml文件中。这是我的示例App.xaml文件,我刚刚为示例创建了一个名为TextConverter的转换器 有两种方法可以做到这一点,如果您使用其他ResourceDic

我在项目中使用了两次列表框样式,所以我想在资源字典中使用它。这个列表框样式中有两个值转换器,所以我在同一个资源文件中实例化了这些转换器。虽然在mainwindow.xaml文件中使用相同的转换器声明时也可以使用,但在运行时它表示“无法声明未知类型”


有人有想法吗?

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

有两种方法可以做到这一点,如果您使用其他ResourceDictionary,则在所有情况下都必须使用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>

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

有两种方法可以做到这一点,如果您使用其他ResourceDictionary,则在所有情况下都必须使用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>

你可以发布一些代码(带有转换器声明的listboxstyle)吗?我看没有问题。你可以发布一些代码(带有转换器声明的listboxstyle)吗?我看没有问题。这对我也有帮助。它也不需要在App.xaml字典中。关键是将转换器包装在标签中,并将其放在资源或合并字典中。这对我也有帮助。它也不需要在App.xaml字典中。关键是将转换器包装在标记中,并将其放置在资源或合并字典中。