基于对象枚举值将图像绑定到ComboBox

时间:2014-08-26 06:51:25

标签: c# wpf image binding valueconverter

我有一个Character类,其属性为Enum。我想让我的代码做的是根据我的角色的性别来切换图像(如果角色是男性=男性形象)。

我遇到问题的方法是如何正确访问并获取此枚举值。你看,我正在创建一个Character类型的ObservableCollection。然后我将此集合绑定到ComboBox,然后通过ItemTemplate显示DisplayMemberPath。

我相信我正在做所有绑定权限,但我没有在我的值转换器中正确访问Character Gender属性。

XAML:参考转换器&&组合框

xmlns:converters="clr-namespace:ImageViaGenderTest_2014"
Title="MainWindow" Height="350" Width="1004.932">
<Window.Resources>
    <converters:GenderToIconFilenameConverter x:Key="IconConverter"/>
</Window.Resources>


 <ComboBox x:Name="Character_ComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="328" Height="25">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="50" Text="{Binding Name}"/>
                        <TextBlock Width="50" Text="{Binding Level}"/>
                        <Image Source="{Binding Gender, Converter={StaticResource IconConverter}}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

MainWindow中的Observable Collection设置

  public ObservableCollection<Character> squad_members = new ObservableCollection<Character>();

    public MainWindow()
    {
        InitializeComponent();

        squad_members.Add(new Character() { Name = "john_snow_knows_nothing", Level = 8,  _Gender = Gender.Male});
        squad_members.Add(new Character() { Name = "Ygritt_gets_wrecked", Level = 4,  _Gender = Gender.Female});

        Binding comboBinding = new Binding();
        comboBinding.Source = squad_members;

        BindingOperations.SetBinding(Character_ComboBox, ComboBox.ItemsSourceProperty, comboBinding);
    }

自定义值转换器类(我认为我错误地引用了性别枚举)错误区域:

[ValueConversion(typeof(Gender), typeof(Image))]
public class GenderToIconFilenameConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
            switch ((Gender)value)
            {
                case Gender.Female:
                    return "character1.jpg";
                case Gender.Male:
                    return "character2.jpg";
                default:
                    return null;
            }

    }

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

角色等级:

public class Character 
{
    public string Name { get; set; }
    public int Level { get; set; }
    public Gender _Gender { get; set; }
}

性别枚举:

public enum Gender
{
    Male, Female
}

我是否错误地引用了我的角色性别枚举?或者还有别的我忘记了吗? 我也从这个问题得到了帮助: Bind Icon depending on Enum in WPF Treeview

1 个答案:

答案 0 :(得分:2)

纯xaml中有一个解决方案

    <ComboBox x:Name="Character_ComboBox"
              HorizontalAlignment="Left"
              VerticalAlignment="Top"
              Width="328"
              Height="25">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="50"
                               Text="{Binding Name}" />
                    <TextBlock Width="50"
                               Text="{Binding Level}" />
                    <Image x:Name="gender" />
                </StackPanel>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Gender}"
                                 Value="Female">
                        <Setter Property="Source"
                                TargetName="gender"
                                Value="character1.jpg" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Gender}"
                                 Value="Male">
                        <Setter Property="Source"
                                TargetName="gender"
                                Value="character2.jpg" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

这种方法不需要任何转换器,当您选择较少时,这会更有用。

这里发生的是我们在模板上指定了DataTrigger并检查了Gender属性的值,当它满足某些条件时,我们相应地设置了图像源。