Default text for the combobox to be checked

I am using an Xceed checkable combobox . Now I want to display the default text depending on the selected checkboxes in the combo box, but I do not know how to do this.

For instance:

enter image description here

The content (red arrow) of the text field should be:

  • If nothing is selected: "Please select"
  • If all is selected: "All people"
  • If one or more is selected: "Specific selection"

how

enter image description here


Example code:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <xctk:CheckComboBox x:Name="_checkComboBox"
                          Height="22"
                          VerticalAlignment="Stretch"
                          ItemsSource="{Binding Names}"
                          SelectedItemsOverride="{Binding SelectedNames}"
                          DisplayMemberPath="Title"
                          Delimiter=", "
                          Width="100"/>
    </Grid>
</Window>

CS:

using System.Windows;

namespace WpfApplication1
{
    using System.Collections.ObjectModel;

    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            _checkComboBox.DataContext = this;

            Names = new ObservableCollection<People>()
              {
                new People() { Title = "Mikel" },
                new People() { Title = "Tom" },
                new People() { Title = "Jennifer" },
                new People() { Title = "Megan" },
              };

            SelectedNames = new ObservableCollection<People>();
        }

        public ObservableCollection<People> Names
        {
            get;
            set;
        }

        public ObservableCollection<People> SelectedNames
        {
            get;
            set;
        }
    }

    public class People
    {
        public string Title
        {
            get;
            set;
        }
    }
}
+4
source share
1 answer

Use a value converter.

Something like this should work:

XAML:

SelectedItemsOverride="{Binding SelectedNames, 
                        Converter={StaticResource SelectedNamesConverter}},
                        ConverterParameter={Binding Names}}"

WITH#:

public class SelectedNamesConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((List<string>)value.length() == 0):
            return "Please select";

        if (((List<string>)value).length() == ((List<string>)parameter).length())
            return "All people";

        return "Specific selection";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Log this as a non-fatal error.  You shouldn't be here!
        return DependencyProperty.UnsetValue;

        // Alternatively:
        // throw new NotImplementedException();
    }
}

And make the names dependent:

    public static DependencyProperty NamesProperty = DependencyProperty.Register(
        "Names",
        typeof(ObservableCollection<People>),
        typeof(MainWindow));

    public ObservableCollection<People> Names
    {
        get { return (ObservableCollection)GetValue(NamesProperty); }
        private set { SetValue(NamesProperty, value); }
    }

MainWindow DependencyObject:

public class MainWindow : DependencyObject
+5

All Articles