Search code examples
wpfconverterstextblockmahapps.metro

WPF; Convert simple string into my TextBlock Text failed and due to Cannot find resource named


So i have this TextBlock:

<TextBlock 
    Name="tbVersion"
    Text="{Binding Converter={StaticResource TextConverter}}"/>

Converter:

public class TextConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return "bla bla";
    }

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

And i got this error when try to run my application:

{"Cannot find resource named 'TextConverter'. Resource names are case sensitive."}

Although there is no compiler errors at all and i declare this:

<Window.Resources>
    <Convertors:TextConverter x:Key="TextConverter"/>
</Window.Resources>

Ok so i try to take another Converter that works from another TextBlobk and still got this error that cannot find resource although this exist (and works as i mentioned...) so maybe because my TextBlobk is inside DataTemplate :

<Controls:MetroWindow.TitleTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock 
                Name="tbVersion"
                Text="{Binding Converter={StaticResource TextConverter}}"/>
        </StackPanel>
    </DataTemplate>
</Controls:MetroWindow.TitleTemplate>

Solution

  • tested code As you can see from the image, i used exactly your code, and is working just fine.

    So you may have added the converter to Window.Resources but you are using it in a different ResourceDictionary?

    If you want to make them globally usable you should add them to the App.xaml, or programmatically to the Application.Current.ResourceDictionary

    EDIT:

    Try adding the converter to the DataTemplate.Resources