在 CheckComboBox 中设置勾选项

Set checked items in CheckComboBox

我正在使用 Xceed checkable combobox。 我需要将几个项目设置为选中项目:

<xctk:CheckComboBox x:Name="cbFileType" DisplayMemberPath="Name" SelectedMemberPath="IsChecked"></xctk:CheckComboBox>

C#代码:

public partial class DataSelector : UserControl
    {
        public class BoolStringClass
        {
            public string Name { get; set; }
            public bool IsChecked { get; set; }
        }
        public BackupDataSelector()
        {
            InitializeComponent();            
            cbFileType.Items.Add(new BoolStringClass { Name = ".jpg", IsChecked = true });
            cbFileType.Items.Add(new BoolStringClass { Name = ".bmp", IsChecked = false });
        }
    }

但“.jpg”项未选中:

如何将“.jpg”设置为选中项?

您应该将要选择的项目添加到 CheckComboBoxSelectedItems 集合中:

cbFileType.Items.Add(new BoolStringClass { Name = ".jpg", IsChecked = true });
cbFileType.Items.Add(new BoolStringClass { Name = ".bmp", IsChecked = false });

foreach (var selectedItem in cbFileType.Items.OfType<BoolStringClass>().Where(x => x.IsChecked))
    cbFileType.SelectedItems.Add(selectedItem);

用户 mm8 的解决方案有效,但不是 MVVM 方式...

来自文档:

https://xceed.com/wp-content/documentation/xceed-toolkit-plus-for-wpf/Xceed.Wpf.Toolkit~Xceed.Wpf.Toolkit.CheckComboBox.html

https://xceed.com/wp-content/documentation/xceed-toolkit-plus-for-wpf/Xceed.Wpf.Toolkit~Xceed.Wpf.Toolkit.Primitives.Selector~SelectedMemberPath.html

您将需要一个 ObservableCollection 和一个 class 保存触发 notifychanged 事件的 IsChecked 和 Item 属性。

下面的工作示例:当您单击 Set A and C 按钮时,将按预期检查项目 AC

MainWindow.xaml.cs

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;

namespace WpfApp6
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel viewModel;

        public MainWindow()
        {
            InitializeComponent();

            viewModel = new ViewModel();

            foreach (var item in new string[] { "A", "B", "C", "D"})
            {
                viewModel.CheckComboBoxItems.Add(new CheckComboBoxItems { IsChecked = false, Item = item}); 
            }

            DataContext = viewModel;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {   
            foreach (var checkBoxToSet in viewModel.CheckComboBoxItems)
            {
                if (checkBoxToSet.Item.Equals("A") || checkBoxToSet.Item.Equals("C"))
                {
                    checkBoxToSet.IsChecked = true;
                }
            }
        }
    }

    public class ViewModel
    {
        public ObservableCollection<CheckComboBoxItems> CheckComboBoxItems { get; set; } = new ObservableCollection<CheckComboBoxItems>();
    }

    public class CheckComboBoxItems : INotifyPropertyChanged
    {
        private bool _isChecked;
        private string _item;

        public bool IsChecked
        {
            get
            {
                return _isChecked;
            }
            set
            {
                _isChecked = value;
                NotifyPropertyChanged("IsChecked");
            }
        }


        public string Item
        {
            get
            {
                return _item;
            }
            set
            {
                _item = value;
                NotifyPropertyChanged("Item");
            }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

MainWindow.xaml

<Window x:Class="WpfApp6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp6"
         xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <xctk:CheckComboBox ItemsSource="{Binding CheckComboBoxItems}" DisplayMemberPath="Item" SelectedMemberPath="IsChecked" Height="50" Width="150"/>
        <Button Content="Set A und C" Width="150" Height="50" Margin="20" Click="Button_Click"/>
    </StackPanel>
</Window>