Question

I got a error "The resource "ComponentTypeToStringConverter could not be resolved" Can someone tell me what am I doing wrong?

I've got this combo box:

<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>

Resource:

<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 (namespace 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();
        }
}
Was it helpful?

Solution

In design time. Your resources are not yet compiled and the current XAML file that you have where it has the ComboBox can't see the ResourceDictionary.

Assuming your resources is defined in App.xaml then you should have no problems with it when you run it in runtime as it'll be able to find the key.

If you want to get rid of the error in design time then what you can do is on your XAML file where the ComboBox lives, you can add the ResourceDictioanry so that it'll be able to find it.

Assuming this is a Window and you have ResourceDictionary that is not defined in the App.xaml but as a separate file

<Window.Resources>
 <ResourceDictionary Source=""/>
</Window.Resources>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top