4

我正在使用Xceed 可检查组合框。现在我想根据组合框中选定的复选框显示默认文本,但我不知道该怎么做。

例如:

在此处输入图像描述

文本框的内容(红色箭头)应为:

  • 如果未选择任何内容:“请选择”
  • 如果选择了所有内容:“所有人”
  • 如果选择一个或多个:“特定选择”

像:

在此处输入图像描述


示例代码:

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

1 回答 1

5

使用价值转换器。

像这样的东西应该工作:

XAML:

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

C#:

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();
    }
}

并使名称成为依赖属性:

    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
于 2014-11-04T08:01:09.563 回答