2

我有一个来自 wpf 工具包 2 的复选框。我无法获得所有选定的项目。我读到我应该使用 SelectedItemsOverride 来获取我选择的所有项目,但它似乎不起作用。我在 TestClassSelected 的设置器中设置了一个断点,但它从未被触发。有任何想法吗?

<xctk:CheckListBox Name="MyCheckList"
                   ItemsSource="{Binding TestClassCollection}"
                   DisplayMemberPath="DisplayName"
                   SelectedItemsOverride="{Binding TestClassSelected}"  />

.

public IEnumerable<TestClass> TestClassCollection
{
    get { return _testClassCollection; }
    set
    {
        _testClassCollection = value;
        OnPropertyChanged("TestClassCollection");
    }
}

public List<TestClass> TestClassSelected
{
    get { return _testClassSelected; }
    set
    {
        _testClassSelected = value;
        OnPropertyChanged("TestClassSelected");
    }
}

.

public class TestClass
{
    public String FirstName { get; set; }
    public String LastName { get; set; }

    public String DisplayName { 
         get { return string.Format("{0} {1}", FirstName, LastName); } 
    }
}

这是我一直在使用的测试数据。

TestClassCollection = new List<TestClass>
{
    new TestClass {FirstName = "FIrstName", LastName = "LastName"},
    new TestClass {FirstName = "Brad", LastName = "Holder"},
    new TestClass {FirstName = "Sam", LastName = "Ryans"},
    new TestClass {FirstName = "Ryan", LastName = "Thomas"},
    new TestClass {FirstName = "Lee", LastName = "Rod"},
    new TestClass {FirstName = "Amanda", LastName = "Gustaf"},
    new TestClass {FirstName = "Chris", LastName = "Holems"},
    new TestClass {FirstName = "Doug", LastName = "Schnitzel"},
    new TestClass {FirstName = "Lisa", LastName = "Bull"},
    new TestClass {FirstName = "Fred", LastName = "Simpson"},
    new TestClass {FirstName = "Scott", LastName = "Rogers"}
};
4

3 回答 3

2

尝试在绑定时添加 Mode 和 UpdateSourceTrigger

<xctk:CheckListBox Name="MyCheckList"
                   ItemsSource="{Binding TestClassCollection}"
                   DisplayMemberPath="DisplayName"
                   SelectedItemsOverride="{Binding TestClassSelected, 
                          Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

更新: 我在Extended WPF Toolkit CodePlex 站点检查了已关闭问题的示例代码。尝试将 TestClassSelected 属性更改为 ObservableCollection。(仍然保留上面在 .xaml 中描述的 UpdateSourceTrigger)

public ObservableCollection<TestClass> TestClassSelected
于 2013-11-05T20:42:51.943 回答
1

我结合了以前的答案/评论,对我有用的是绑定SelectedItemsOverride到一个ObservableCollection<T>,包含UpdateSourceTrigger=PropertyChanged,并将一个方法附加到ObservableCollection<T>的事件CollectionChanged

XAML:

<xctk:CheckListBox 
    ItemsSource="{Binding AllItems}"
    DisplayMemberPath="Display"
    ValueMemberPath="Value" 
    SelectedItemsOverride="{Binding Path=DataContext.SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
</xctk:CheckListBox>

ViewModel 中的代码:

public IEnumerable<Item> AllItems { get; set; }

public ObservableCollection<Item> SelectedItems { get; set; }

public ViewModel()
{
    SelectedItems = new ObservableCollection<Item>();

    SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;
}

private void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    // Handle collection changed event
}
于 2020-01-14T12:42:35.160 回答
0

我遇到了同样的问题并通过分配属性的私有字段来解决它

在你的例子中

private ObservableCollection<TestClass> _testClassSelected=new ObservableCollection<TestClass>();
于 2018-08-07T08:14:58.363 回答