1

I'm currently using the following code to build a ComboBox control with flag images and canadian province names. However the images are not showing up in the control. I have tested the binding and it generated the location properly, but the image just doesn't come up in the control.

Not sure what is wrong here any help would be appreciated

Code:

<ComboBox x:Name="cb_Provinces" Text="Province"SelectionChanged="ComboBox_SelectionChanged"  SelectedValuePath="ProvinceCode" ItemsSource="{Binding Provinces, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
    <ComboBox.ItemTemplate>
        <DataTemplate >
            <StackPanel>
                <StackPanel x:Name="stk_ComboTemplate" Orientation="Horizontal" HorizontalAlignment="Left">
                    <Image Width="25" Margin="10" Source="{Binding ProvinceCode, StringFormat=/CanadaTreeSvc.Interface;component/Resources/img/flags/\{0\}.gif}" />

                    <TextBlock Text="{Binding ProvinceName}"/>

                </StackPanel>
                <TextBlock FontSize="10" Foreground="Gray" Text="{Binding ProvinceCode, StringFormat=/CanadaTreeSvc.Interface;component/Resources/img/flags/\{0\}.gif}"/>

            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>

Resulting Output:

enter image description here

2
  • Did you try changing {0\} to {0}\? Commented Mar 21, 2013 at 4:52
  • StringFormat is only used if the target property is actually a string, The Image Source property is a Uri so the binding won't apply the StrngFormat. You will have to use a IValueConverter Commented Mar 21, 2013 at 5:07

1 Answer 1

2

StringFormat only works if the target is of type String. Because Image Source is of type Uri the StringFormat will never be used in the Binding

The best option would be to make a IValueConverter to format the string and return it to the Image Source property.

Example:

public class ProvinceNameToImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Format("/CanadaTreeSvc.Interface;component/Resources/img/flags/\{0\}.gif", value);
    }

    public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

Usage:

<Window.Resources>
    <local:ProvinceNameToImageSourceConverter x:Key="ImageConverter" />
</Window.Resources>

..................

   <Image Source="{Binding ProvinceCode, Converter={StaticResource ImageConverter}}" />
Sign up to request clarification or add additional context in comments.

2 Comments

this worked but i don't understand why the original doesn't work here when i have used a similar method to do this in the past for dice game i wrote a few weeks ago using this <Button Name="TheButton"> <Image Name="img_Die" Source="{Binding Value, StringFormat=/BluffersDice.Interface;component/res/dice/{0:d}.png }" /> </Button>
so would you put the c# code in the code behind, what about a more global solution ?

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.