为什么我的 SelectedItems 依赖项 属性 总是 returns null 绑定 属性

why my SelectedItems dependency property always returns null to bound property

我创建了一个 UserControl1,它包装了一个 DataGrid(这是为了测试目的而简化的,真实场景涉及第三方控件,但问题是一样的)。 UserControl1 在测试应用程序的主窗口中使用,如下所示:

<test:UserControl1 ItemsSource="{Binding People,Mode=OneWay,ElementName=Self}"
                             SelectedItems="{Binding SelectedPeople, Mode=TwoWay, ElementName=Self}"/>

除了在 DataGrid 中选择一行时,SelectedPeople 属性 始终设置为空。

除外,一切正常。

行选择流程大致为:UserControl1.DataGrid -> UserControl1.DataGrid_OnSelectionChanged -> UserControl1.SelectedItems -> MainWindow.SelectedPeople

调试显示带有来自 DataGrid 的选定项的 IList 正在传递给 SelectedItems 依赖项的 SetValue 调用 属性。但是当随后调用 SelectedPeople setter(作为绑定过程的一部分)时,传递给它的值始终为 null。

这是相关的 UserControl1 XAML:

<Grid>
    <DataGrid x:Name="dataGrid" SelectionChanged="DataGrid_OnSelectionChanged" />
</Grid>

在 UserControl1 的代码隐藏中是 SelectedItems 依赖属性和 DataGrid SelectionChanged 处理程序的以下定义:

    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(UserControl1), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemsChanged));
    public IList SelectedItems
    {
        get { return (IList)GetValue(SelectedItemsProperty); }

        set
        {
            SetValue(SelectedItemsProperty, value);
        }
    }

    private bool _isUpdatingSelectedItems;

    private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var ctrl = d as UserControl1;

        if ((ctrl != null) && !ctrl._isUpdatingSelectedItems)
        {
            ctrl._isUpdatingSelectedItems = true;

            try
            {
                ctrl.dataGrid.SelectedItems.Clear();
                var selectedItems = e.NewValue as IList;

                if (selectedItems != null)
                {
                    var validSelectedItems = selectedItems.Cast<object>().Where(item => ctrl.ItemsSource.Contains(item) && !ctrl.dataGrid.SelectedItems.Contains(item)).ToList();
                    validSelectedItems.ForEach(item => ctrl.dataGrid.SelectedItems.Add(item));
                }
            }
            finally
            {
                ctrl._isUpdatingSelectedItems = false;
            }
        }
    }

    private void DataGrid_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!_isUpdatingSelectedItems && sender is DataGrid)
        {
            _isUpdatingSelectedItems = true;

            try
            {
                var x = dataGrid.SelectedItems;
                SelectedItems = new List<object>(x.Cast<object>());
            }
            finally
            {
                _isUpdatingSelectedItems = false;
            }
        }
    }

这是 MainWindow 代码隐藏中 SomePeople 的定义:

    private ObservableCollection<Person> _selectedPeople;
    public ObservableCollection<Person> SelectedPeople
    {
        get { return _selectedPeople; }
        set { SetProperty(ref _selectedPeople, value); }
    }    

    public class Person
    {
        public Person(string first, string last)
        {
            First = first;
            Last = last;
        }

        public string First { get; set; }
        public string Last { get; set; }
    }

我遇到了同样的问题,我不知道原因,但我是这样解决的:

1) DP

public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(object), typeof(UserControl1),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemsChanged));

    public object SelectedItems
    {
        get { return (object) GetValue(SelectedItemsProperty); }
        set { SetValue(SelectedItemsProperty, value); }
    }

2) 网格事件

private void DataGrid_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var SelectedItemsCasted = SelectedItems as IList<object>;
        if (SelectedItemsCasted == null)
            return;

        foreach (object addedItem in e.AddedItems)
        {
            SelectedItemsCasted.Add(addedItem);
        }

        foreach (object removedItem in e.RemovedItems)
        {
            SelectedItemsCasted.Remove(removedItem);
        }
    }

3) 在包含UserControl1的UC中

属性:

public IList<object> SelectedPeople { get; set; }

构造函数:

    public MainViewModel()
    {
        SelectedPeople = new List<object>();
    }

我知道这是一个非常古老的 post- 但在深入研究这个以及其他一些解决这个问题的 post 之后,我找不到一个完整的工作解决方案。因此,根据 post 中的概念,我正在这样做。

我还使用完整的演示项目创建了一个 GitHub 存储库,其中包含比此 post 更多的评论和逻辑解释。 MultiSelectDemo

我能够创建一个 AttachedProperty(还有一些 AttachedBehavour 逻辑来设置 SelectionChanged 处理程序)。

MultipleSelectedItemsBehaviour

public class MultipleSelectedItemsBehaviour
{
    public static readonly DependencyProperty MultipleSelectedItemsProperty =
        DependencyProperty.RegisterAttached("MultipleSelectedItems", typeof(IList), typeof(MultipleSelectedItemsBehaviour),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, MultipleSelectedItemsChangedCallback));

    public static IList GetMultipleSelectedItems(DependencyObject d) => (IList)d.GetValue(MultipleSelectedItemsProperty);
    public static void SetMultipleSelectedItems(DependencyObject d, IList value) => d.SetValue(MultipleSelectedItemsProperty, value);

    public static void MultipleSelectedItemsChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (sender is DataGrid dataGrid)
        {
            if (e.NewValue == null)
            {
                dataGrid.SelectionChanged -= DataGrid_SelectionChanged;
            }
            else
            {
                dataGrid.SelectionChanged += DataGrid_SelectionChanged;
            }
        }
    }

    private static void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (sender is DataGrid dataGrid)
        {
            var selectedItems = GetMultipleSelectedItems(dataGrid);

            if (selectedItems == null) return;

            foreach (var item in e.AddedItems)
            {
                try
                {
                    selectedItems.Add(item);
                }
                catch (ArgumentException)
                {

                }
            }

            foreach (var item in e.RemovedItems)
            {
                selectedItems.Remove(item);
            }
        }
    }
}

要使用它,视图模型中的一件关键事情是必须初始化视图模型集合,以便附加的 property/behaviour 设置 SelectionChanged 处理程序。在此示例中,我在 VM 构造函数中完成了该操作。

public MainWindowViewModel()
{
    MySelectedItems = new ObservableCollection<MyItem>();
}

private ObservableCollection<MyItem> _myItems;
public ObservableCollection<MyItem> MyItems
{
    get => _myItems;
    set => Set(ref _myItems, value);
}

private ObservableCollection<MyItem> _mySelectedItems;
public ObservableCollection<MyItem> MySelectedItems
{
    get => _mySelectedItems;
    set
    {
        // Remove existing handler if there is already an assignment made (aka the property is not null).
        if (MySelectedItems != null)
        {
            MySelectedItems.CollectionChanged -= MySelectedItems_CollectionChanged;
        }

        Set(ref _mySelectedItems, value);

        // Assign the collection changed handler if you need to know when items were added/removed from the collection.
        if (MySelectedItems != null) 
        {
            MySelectedItems.CollectionChanged += MySelectedItems_CollectionChanged;
        }
    }
}

private int _selectionCount;
public int SelectionCount
{
    get => _selectionCount;
    set => Set(ref _selectionCount, value);
}

private void MySelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    // Do whatever you want once the items are added or removed.
    SelectionCount = MySelectedItems != null ? MySelectedItems.Count : 0;
}

最后在 XAML

中使用它
<DataGrid Grid.Row="0"
          ItemsSource="{Binding MyItems}"
          local:MultipleSelectedItemsBehaviour.MultipleSelectedItems="{Binding MySelectedItems}" >
    
</DataGrid>