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.

ChrisUK
  • 547
  • 8
  • 17
  • 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. – Clemens Nov 20 '17 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. – ChrisUK Nov 20 '17 at 13:08

1 Answers1

12

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>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • OK thanks, but how would I write it if I already have within Application.Resources? Sorry, I'm still very new to this. – ChrisUK Nov 20 '17 at 13:02
  • 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. – ChrisUK Nov 20 '17 at 13:21