从列表框中获取选中的项目

破解文件

我只是习惯了 MVVM 并希望没有代码隐藏并在视图模型中定义所有内容。组合框代表几个选择选项(作品)。我想查询已检查的元素。不幸的是,我无法访问它们。文本框应将所有选定元素显示为串联字符串。

视图模型

class MainViewModel : BaseViewModel
    {
        #region Fields

        private ObservableCollection<EssayTypeViewModel> _essayTypes;
        private EssayTypeViewModel _selectedEssayTypes;

        #endregion


        public ObservableCollection<EssayTypeViewModel> EssayTypes
        {
            get => _essayTypes;
            set
            {
                if (_essayTypes == value) return;
                _essayTypes = value; OnPropertyChanged("EssayTypes");
            }
        }


        public EssayTypeViewModel SelectedEssayTypes
        {
            get => _selectedEssayTypes;
            set { _selectedEssayTypes = value; OnPropertyChanged("SelectedEssayTypes"); }
        }


        public MainViewModel()
        {

            // Load Essay Types
            EssayTypeRepository essayTypeRepository = new EssayTypeRepository();
            var essayTypes = essayTypeRepository.GetEssayTypes();
            var essayTypeViewModels = essayTypes.Select(m => new EssayTypeViewModel()
            {
                Text = m.Text
            });

            EssayTypes = new ObservableCollection<EssayTypeViewModel>(essayTypeViewModels);

        }
    }

XAML

<ListBox x:Name="Listitems" SelectionMode="Multiple" Height="75" Width="200" ItemsSource="{Binding EssayTypes}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Text}" IsChecked="{Binding Checked}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <TextBox Text="{Binding Path=SelectedEssayTypes}" Grid.Column="0" Width="160" Height="25" Margin="0,140,0,0"/>
毫米8

您可以将事件处理程序连接到集合PropertyChanged中所有EssayTypeViewModel对象事件,EssayTypesPropertyChangedMainViewModel将所有选定元素作为连接字符串返回的只读属性引发事件

public MainViewModel()
{
    // Load Essay Types
    EssayTypeRepository essayTypeRepository = new EssayTypeRepository();
    var essayTypes = essayTypeRepository.GetEssayTypes();
    var essayTypeViewModels = essayTypes.Select(m =>
    {
        var vm = EssayTypeViewModel()
        {
            Text = m.Text
        };
        vm.PropertyChanged += OnPropertyChanged;
        return vm;
    });

    EssayTypes = new ObservableCollection<EssayTypeViewModel>(essayTypeViewModels);
}

private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Checked")
        OnPropertyChanged("SelectedItems");
}

public string SelectedItems => string.Join(",", EssayTypes.Where(x => x.Checked).ToArray());

这需要 EssayTypeViewModel 类来实现INotifyPropertyChanged接口(例如通过从您的BaseViewModel派生)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用C#从列表框中的选定项目中获取ValueMember

来自分类Dev

C#获取选中的双击列表框

来自分类Dev

遍历列表框中的项目

来自分类Dev

如何在选中的列表框中设置项目之间的空间?

来自分类Dev

在列表框中命名项目

来自分类Dev

从列表框项目获取文本

来自分类Dev

播放列表框中的项目

来自分类Dev

使某些Tkinter列表框项目始终处于选中状态

来自分类Dev

VBA AutoFilter用于选中的列表框项目

来自分类Dev

如何将选中的项目移动到列表框

来自分类Dev

在wpf xaml中单击获取列表框项目的名称

来自分类Dev

如何使用pywinauto选中/取消选中列表框中的项目?

来自分类Dev

通过选定的项目(列表框项目)获取控件(列表框)的名称-MVVM

来自分类Dev

从“保留”事件中的“ Windows Phone”列表框中获取选定的项目

来自分类Dev

选中的列表框-选中的项目

来自分类Dev

如何通过单击鼠标来获取文本框中的列表框项目?

来自分类Dev

在列表框中绑定项目控件

来自分类Dev

从列表框中获取所选项目

来自分类Dev

选中的列表框特定项目双击Windows Form Application上的DataGridView时选中

来自分类Dev

获取列表框中项目的高度

来自分类Dev

在列表框中显示默认项目

来自分类Dev

WPF绑定选中列表框的“项目属性”

来自分类Dev

如何获取给定索引号的列表框中项目的ValueMember?

来自分类Dev

从选中的列表框中检索选定的项目?

来自分类Dev

从列表框获取项目并将其放入列中

来自分类Dev

使用jQuery从asp列表框中获取选定项目的计数

来自分类Dev

如何从列表框所选项目中获取要在 Picturbox 中显示的图像

来自分类Dev

在 MVVM 中获取列表框 SelectedValue

来自分类Dev

访问:从列表框中获取价值

Related 相关文章

  1. 1

    使用C#从列表框中的选定项目中获取ValueMember

  2. 2

    C#获取选中的双击列表框

  3. 3

    遍历列表框中的项目

  4. 4

    如何在选中的列表框中设置项目之间的空间?

  5. 5

    在列表框中命名项目

  6. 6

    从列表框项目获取文本

  7. 7

    播放列表框中的项目

  8. 8

    使某些Tkinter列表框项目始终处于选中状态

  9. 9

    VBA AutoFilter用于选中的列表框项目

  10. 10

    如何将选中的项目移动到列表框

  11. 11

    在wpf xaml中单击获取列表框项目的名称

  12. 12

    如何使用pywinauto选中/取消选中列表框中的项目?

  13. 13

    通过选定的项目(列表框项目)获取控件(列表框)的名称-MVVM

  14. 14

    从“保留”事件中的“ Windows Phone”列表框中获取选定的项目

  15. 15

    选中的列表框-选中的项目

  16. 16

    如何通过单击鼠标来获取文本框中的列表框项目?

  17. 17

    在列表框中绑定项目控件

  18. 18

    从列表框中获取所选项目

  19. 19

    选中的列表框特定项目双击Windows Form Application上的DataGridView时选中

  20. 20

    获取列表框中项目的高度

  21. 21

    在列表框中显示默认项目

  22. 22

    WPF绑定选中列表框的“项目属性”

  23. 23

    如何获取给定索引号的列表框中项目的ValueMember?

  24. 24

    从选中的列表框中检索选定的项目?

  25. 25

    从列表框获取项目并将其放入列中

  26. 26

    使用jQuery从asp列表框中获取选定项目的计数

  27. 27

    如何从列表框所选项目中获取要在 Picturbox 中显示的图像

  28. 28

    在 MVVM 中获取列表框 SelectedValue

  29. 29

    访问:从列表框中获取价值

热门标签

归档