4

I've created a multibinding converter (ListItemDescriptionConverter) that will combine several values into a single string as output for ListBox items. However I don't know how to get the resource dictionary to point to the converter class in a separate .cs file. It cannot be found when I use the following markup:

            <TextBlock Style="{StaticResource BasicTextStyle}">
                <TextBlock.Text>
                    <MultiBinding Converter="StaticResource {ListItemDescriptionConverter}">
                        <Binding Path="Genres"></Binding>
                        <Binding Path="Year"></Binding>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>

Is there something else I must do within the resource dictionary to access the converter class? I cannot add the reference within Window.Resources as it needs to be within a resource dictionary so I can reuse the style throughout my app.

2
  • 1
    Converter="{ListItemDescriptionConverter}" should be Converter="{StaticResource ListItemDescriptionConverter}". Of course the converter should be declared as a resource as usual, i.e. the same way you did with BasicTextStyle. Commented Nov 20, 2017 at 12:48
  • Sorry yes you're right, I must have deleted it by accident when I was messing around with the code to get it to work. I've updated the OP accordingly. Commented Nov 20, 2017 at 13:08

1 Answer 1

13

Define the converter as a resource, for example in your App.xaml:

<Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication8" 
             StartupUri="MainWindow.xaml">
   <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins\DefaultSkinDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <local:ListItemDescriptionConverter x:Key="ListItemDescriptionConverter" />
    </ResourceDictionary>
    </Application.Resources>
</Application>

You can then reference it using the StaticResource markup extension and the x:Key:

<MultiBinding Converter="{StaticResource ListItemDescriptionConverter}">

The other option is to set the Converter property to an instance of your converter class using element syntax:

<MultiBinding>
    <MultiBinding.Converter>
        <local:ListItemDescriptionConverter />
    </MultiBinding.Converter>
</MultiBinding>
Sign up to request clarification or add additional context in comments.

2 Comments

OK thanks, but how would I write it if I already have <ResourceDictionary Source="Skins\DefaultSkinDictionary.xaml"/> within Application.Resources? Sorry, I'm still very new to this.
Yes I think that's what I'm after as I get no more design time errors. Unfortunately I am now getting "An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll" when I run the app - however this may be unrelated to the issue raised in this thread., so I'll accept your answer. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.