从ListBox SelectedItems属性中检索枚举

时间:2015-07-27 14:59:40

标签: .net wpf xaml mvvm enums

我定义了一个Enum,我的目的是向ListBox中的用户显示四个选项(None,Left,Center和Right)。此ListBox将允许多个选择。单击save命令时,我必须将选择传递给ViewModel,在那里我将聚合选择并将其传递给WCF服务。

枚举:

[DataContract]
[Flags]
public enum Locations
{
    None = 0,
    [EnumMember]
    Left = 1,
    [EnumMember]
    Center = 2,
    [EnumMember]
    Right = 4,
    [EnumMember]
    LeftCenter = Left | Center,
    [EnumMember]
    LeftRight = Left | Right,
    [EnumMember]
    CenterRight = Center | Right,
    [EnumMember]
    All = Left | Center | Right
}

XAML:

<Button Command="{Binding SaveCommand}"
        CommandParameter="{Binding SelectedItems, ElementName=lbLocations}" />
<ListBox x:Name="lbLocations" SelectionMode="Multiple">
    <ListBoxItem Content="{x:Static m:Subsections.None}" />
    <ListBoxItem Content="{x:Static m:Subsections.Left}" />
    <ListBoxItem Content="{x:Static m:Subsections.Center}" />
    <ListBoxItem Content="{x:Static m:Subsections.Right}" />
</ListBox>

视图模型:

public ICommand SaveCommand
{
    get
    {
        if (_saveCommand == null)
            _saveCommand = new RelayCommand<IList>(x => Save(x));

         return _saveCommand;
    }
}

private void Save(IList locations)
{
    try
    {
        // ToList() produces InvalidCastException.
        var collection = locations.Cast<Locations>().ToList();

        // Do WCF stuff, display success, etc.
    }
    catch (Exception ex)
    {
        _dialogService.Show(ex.Message, "Error");
    }
}

我已经成功地将选择作为IList传递回我的ViewModel,但是我很难将它转回我的枚举。有没有更好的方法我忽略了,这可行吗?好像我快到了。

2 个答案:

答案 0 :(得分:1)

尝试迭代已转换列表并将值聚合到单个变量中,如下所示:

private void Save(IList locations)
{
    try
    {
        Locations location = Locations.None;

        foreach (Locations value in locations.Cast<Locations>())
        {
            location |= value;
        }

        // Do WCF stuff, display success, etc.
    }
    catch (Exception ex)
    {
        _dialogService.Show(ex.Message, "Error");
    }
}

答案 1 :(得分:0)

有一种更好的方法可以实现这一目标:

创建一个类来保存ListBox

中每个项目的数据
class EnumSelection
{
    public bool IsSelected { get; set; }

    public Subsections Value { get; set; }

    public EnumSelection(Subsections value)
    {
        Value = value;
    }
}

ItemsSource的{​​{1}}属性绑定到可用的枚举

ListBox

使用DataTemplate绑定到新类

private IEnumerable<EnumSelection> enums;
public IEnumerable<EnumSelection> Enums
{
    get
    {
        if (this.enums == null)
        {
            this.enums = Enum.GetValues(typeof(Subsections)).Cast<Subsections>().Select(s => new EnumSelection(s));
        }

        return this.enums;
    }
}

public IEnumerable<EnumSelection> SelectedItems
{
    get
    {
        return Enums.Where(e => e.IsSelected);
    }
}

保存方法:

<ListBox ItemsSource="{Binding Enums}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsSelected}"/>
                <TextBlock Text="{Binding Value}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>