资源无法解决

时间:2014-05-08 19:28:41

标签: c# wpf xaml

我收到错误“资源”ComponentTypeToStringConverter无法解决“有人能告诉我我做错了什么吗?

我有这个组合框:

<ComboBox SelectedItem="{Binding PcComponent.ComponentTypeName}" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2">
      <ComboBox.ItemTemplate>
           <DataTemplate>
                 <TextBlock Text="{Binding Converter={StaticResource ComponentTypeToStringConverter}}"/> <!-- HERE I got a error The resource "ComponentTypeToStringConverter could not be resolved -->
           </DataTemplate>
      </ComboBox.ItemTemplate>
</ComboBox>

资源:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:c="clr-namespace:PcConfigurator.Converters">
    <c:ComponentTypeToStringConverter x:Key="ComponentTypeToStringConverter"/>
</ResourceDictionary>

Converter(名称空间PcConfigurator.Converters):

public class ComponentTypeToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is ComponentType)) { return null; }

            ComponentType type = (ComponentType)value;

            switch (type)
            {
               //Do something
            }

            throw new InvalidOperationException("Enum value is unknown");
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
}

1 个答案:

答案 0 :(得分:7)

设计时间。您的资源尚未编译,并且您拥有XAML的当前ComboBox文件无法查看ResourceDictionary

假设您的资源是在App.xaml中定义的,那么当您在运行时运行它时,它应该没有问题,因为它可以找到key

如果您想摆脱设计时中的错误,那么您可以在XAML所在的ComboBox文件中执行操作,您可以添加{ {1}}以便它能够找到它。

假设这是一个ResourceDictioanry而您Window未在ResourceDictionary中定义,而是作为单独的文件

App.xaml