Reputation: 459
I'm using the Xceed checkable combobox. I need to set a few items as checked items:
<xctk:CheckComboBox x:Name="cbFileType" DisplayMemberPath="Name" SelectedMemberPath="IsChecked"></xctk:CheckComboBox>
C# code:
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 });
}
}
But ".jpg" item is not checked:
How can I set ".jpg" as checked item?
Upvotes: 0
Views: 3886
Reputation: 1052
The solution from user mm8 works, but that not the MVVM way...
From the docs:
And
You'll need an ObservableCollection and a class holding the IsChecked and Item properties that fire the notifychanged event.
Below the working example: When you click the Set A and C
button the items A
and C
will be checked as expected.
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>
Upvotes: 0
Reputation: 169330
You should add the items to be selected to the SelectedItems
collection of the CheckComboBox
:
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);
Upvotes: 1