在CheckComboBox中设置选中的项目

时间:2017-11-02 10:57:06

标签: c# .net wpf xaml xceed

我正在使用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 });
        }
    }

但&#34; .jpg&#34;项目未被选中:

my CheckComboBox

如何设置&#34; .jpg&#34;作为检查项目?

2 个答案:

答案 0 :(得分:1)

您应该将要选择的项目添加到SelectedItems的{​​{1}}集合中:

CheckComboBox

答案 1 :(得分:0)

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和一个包含IsChecked和Item属性的类,这些属性将触发notifychanged事件。

在工作示例下面:单击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>