wpf - 如何在 MVVM 中将标志枚举绑定(bind)到 ListBox

标签 wpf mvvm enums listbox flags

我想将具有标志属性的枚举绑定(bind)到具有 mvvm 模式中的复选框项目模板的列表框?我怎样才能做到这一点?

[Flags]
public enum SportTypes
{
   None = 0,
   Baseball = 1,
   Basketball = 2,
   Football = 4,
   Handball = 8,
   Soccer = 16,
   Volleyball = 32
}


<ListBox Name="checkboxList2"
                 ItemsSource="{Binding Sports}"

                 Margin="0,5" 
                 SelectionMode="Multiple">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=SportTypeEnum, Converter={StaticResource enumBooleanConverter}}" 
                  Content="{Binding Item}"/>
                </DataTemplate>

            </ListBox.ItemTemplate>

最佳答案

您不能轻易地直接绑定(bind)该值,因为转换器无法从单个标志构建标志组合。所以你需要管理一个标志的集合,并基于这个集合构建组合。困难在于ListBox.SelectedItems属性是只读的。但是,this blog post使用附加属性提供了一个很好的解决方法。

这是使用此解决方案的完整示例:

代码隐藏

[Flags]
public enum MyEnum
{
    Foo = 1,
    Bar = 2,
    Baz = 4
}

public partial class TestEnum : Window, INotifyPropertyChanged
{
    public TestEnum()
    {
        InitializeComponent();
        _flags = (MyEnum[])Enum.GetValues(typeof(MyEnum));
        _selectedFlags = new ObservableCollection<MyEnum>();
        _selectedFlags.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_selectedFlags_CollectionChanged);
        this.DataContext = this;
    }

    void _selectedFlags_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (_selectedFlags.Count == 0)
            Value = default(MyEnum);
        else
            Value = _selectedFlags.Aggregate((v, acc) => acc | v);
    }

    private MyEnum[] _flags;
    public MyEnum[] Flags
    {
        get { return _flags; }
        set
        {
            _flags = value;
            OnPropertyChanged("Flags");
        }
    }

    private ObservableCollection<MyEnum> _selectedFlags;
    public ObservableCollection<MyEnum> SelectedFlags
    {
        get { return _selectedFlags; }
        set
        {
            _selectedFlags = value;
            OnPropertyChanged("SelectedFlags");
        }
    }

    private MyEnum _value;
    public MyEnum Value
    {
        get { return _value; }
        set
        {
            _value = value;
            OnPropertyChanged("Value");
            var currentFlags = _flags.Where(f => _value.HasFlag(f));
            var addedFlags = currentFlags.Except(_selectedFlags).ToArray();
            var removedFlags = _selectedFlags.Except(currentFlags).ToArray();
            foreach (var f in addedFlags)
            {
                _selectedFlags.Add(f);
            }
            foreach (var f in removedFlags)
            {
                _selectedFlags.Remove(f);
            }
        }
    }

    private DelegateCommand<MyEnum> _setValueCommand;
    public ICommand SetValueCommand
    {
        get
        {
            if (_setValueCommand == null)
            {
                _setValueCommand = new DelegateCommand<MyEnum>(v => Value = v);
            }
            return _setValueCommand;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML
<Window x:Class="TestPad.TestEnum"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:TestPad"
        Title="TestEnum" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding Value}" />
        <ListBox Grid.Row="1"
                 ItemsSource="{Binding Flags}"
                 SelectionMode="Extended"
                 my:MultiSelectorBehavior.SynchronizedSelectedItems="{Binding SelectedFlags}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding}" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ItemsControl Grid.Row="2"
                      ItemsSource="{Binding Flags}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Command="{Binding DataContext.SetValueCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
                            CommandParameter="{Binding}"
                            Content="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

注意:为简单起见,ViewModel 是 Window 类。在真正的 MVVM 应用程序中,您当然会使用单独的 ViewModel 类...

关于wpf - 如何在 MVVM 中将标志枚举绑定(bind)到 ListBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3474924/

相关文章:

c# - 在 WPF 中播放 mp3

wpf - 如何检查 WPF Prism 中的复选框是否选中

objective-c - 将 0 作为枚举选项传递给 Swift 中的 Objective C 函数

objective-c - 如何像 Apple 在 Objective-C 中那样以 typedef 枚举为基础类行为?

java - Dozer 是否允许从 Enum 转换为 Enum?

c# - WPF:在运行时更改配置文件用户设置?

c# - WPF Application是否需要继承System.Windows.Application基类?

c# - 如何将带空格的字符串传递给 converterParameter?

android - android mvvm大量的观察者

c# - Entity Framework 在重新连接实体时不跟踪集合更改