views:

41

answers:

2

Hi, I have a simple template for a combobox structured in this way:

            <ComboBox DockPanel.Dock="Left" MinWidth="100" MaxHeight="24"
                  ItemsSource="{Binding Actions}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" Width="100" />
                        <Image Source="{Binding Converter={StaticResource TypeConverter}}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

So, if I use this code, everything works:

                            <TextBlock Text="{Binding Name}" Width="100" />
                        <!--<Image Source="{Binding Converter={StaticResource TypeConverter}}" /> -->
                        <Image Source="{StaticResource SecurityImage}" />

But if I use the converter it doesn't work anymore. This is the converter, but I don't know how I can refer to the static resource from there ...

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var type = (Action)value;
        var img = new BitmapImage();
        switch (type.ActionType)
        {
            case ActionType.Security:
                img.UriSource = new Uri("StructureImage", UriKind.Relative);
                break;
            case ActionType.Structural:
                img.UriSource = new Uri("SecurityImage", UriKind.Relative);
                break;
        }

        return img;
    }
A: 

When using Image.UriSource you need to specify the relative file path to your images if the images have been added to your project and their "Build Action" has been set to "Resource". E.g. if you have put your images in a project folder in Visual Studio called "images", you can refer to the images in the following way:

img.UriSource = new Uri("/Images/StructureImage.png", UriKind.Relative);

If the images are not build as a resource, you have to use the full file path i.e.

img.UriSource = new Uri("http://server/Images/StructureImage.png", UriKind.Absolute);

EDIT:

If you put your images in your Application resourcedictionary, you can always access it in the following way:

Application.Current.Resources["StructureImage"];

If you put the resources somewhere else you may use a IMultiValueConverter instead of IValueConverter for your converter. Then your typeconverter would look something like the following:

class TestValueConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // Validation of parameters goes here...

        var type = (Action) values[0];
        var image1 = values[1];
        var image2 = values[2];

        if (type.ActionType == ActionType.Security)
        {
            return image1;
        }
        else
        {
            return image2;
        }
    }

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

    #endregion
}

and your XAML would look similar to this:

    <Image>
        <Image.Source>
            <MultiBinding Converter="{StaticResource testValueConverter}">
                <Binding Path="Action" />
                <Binding Source="{StaticResource SecurityImage}" />
                <Binding Source="{StaticResource StructureImage}" />
            </MultiBinding>
        </Image.Source>
    </Image>

Finally, this would be how you'd define your resources:

<imaging:BitmapImage x:Key="StructureImage" UriSource="StructureImage.png" />
<imaging:BitmapImage x:Key="SecurityImage" UriSource="SecurityImage.png" />
<local:TestValueConverter x:Key="testValueConverter" />

The above code is untested!

Jakob Christensen
Ok, but if I want to refer to them using the key I have in the dictionary entry?
Check my edits.
Jakob Christensen
+1  A: 

Try to use the Switch Converter written by Josh, should work for you:

SwitchConverter –

A "switch statement" for XAML - http://josheinstein.com/blog/index.php/2010/06/switchconverter-a-switch-statement-for-xaml/

No need to write your converter, your code will look like this -

<Grid.Resources>  
    <e:SwitchConverter x:Key="ActionIcons">  
        <e:SwitchCase When="Security" Then="SecurithImage.png" />  
        <e:SwitchCase When="Structural" Then="StructureImage.png" />             
    </e:SwitchConverter>  
</Grid.Resources>  

<Image Source="{Binding Converter={StaticResource ActionIcons}}" />  

akjoshi
I am gonna give it a trythanks
Ok, let me know how it goes.Please post your code in case it works, will help others. Thanks.
akjoshi