实现DataGrid的SelectedItems的绑定
This commit is contained in:
43
DMS.WPF/Helper/SelectedItemsBehavior.cs
Normal file
43
DMS.WPF/Helper/SelectedItemsBehavior.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
using Microsoft.Xaml.Behaviors;
|
||||||
|
|
||||||
|
namespace DMS.WPF.Helper
|
||||||
|
{
|
||||||
|
public class SelectedItemsBehavior : Behavior<DataGrid>
|
||||||
|
{
|
||||||
|
public static readonly DependencyProperty SelectedItemsProperty =
|
||||||
|
DependencyProperty.Register(nameof(SelectedItems), typeof(IList), typeof(SelectedItemsBehavior),
|
||||||
|
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
|
||||||
|
|
||||||
|
public IList SelectedItems
|
||||||
|
{
|
||||||
|
get => (IList)GetValue(SelectedItemsProperty);
|
||||||
|
set => SetValue(SelectedItemsProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnAttached()
|
||||||
|
{
|
||||||
|
base.OnAttached();
|
||||||
|
AssociatedObject.SelectionChanged += OnSelectionChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnDetaching()
|
||||||
|
{
|
||||||
|
base.OnDetaching();
|
||||||
|
if (AssociatedObject != null)
|
||||||
|
{
|
||||||
|
AssociatedObject.SelectionChanged -= OnSelectionChanged;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||||
|
{
|
||||||
|
if (sender is DataGrid dataGrid)
|
||||||
|
{
|
||||||
|
SelectedItems = dataGrid.SelectedItems;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user