web-dev-qa-db-ja.com

MVVMのDataGridまたはListBoxからSelectedItemsにバインド

DataGridからselectedItemsをバインドする必要があるが、具体的なものを思い付くことができないWPFで簡単な読み取りを行っているだけです。選択したオブジェクトが必要です。

DataGrid:

<DataGrid Grid.Row="5" 
    Grid.Column="0" 
    Grid.ColumnSpan="4" 
    Name="ui_dtgAgreementDocuments"
    ItemsSource="{Binding Path=Documents, Mode=TwoWay}"
    SelectedItem="{Binding Path=DocumentSelection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Stretch" 
    Background="White"
    SelectionMode="Extended" Margin="2,5" 
    IsReadOnly="True" 
    CanUserAddRows="False" 
    CanUserReorderColumns="False" 
    CanUserResizeRows="False"
    GridLinesVisibility="None" 
    HorizontalScrollBarVisibility="Hidden"
    columnHeaderStyle="{StaticResource GreenTea}" 
    HeadersVisibility="Column" 
    BorderThickness="2" 
    BorderBrush="LightGray" 
    CellStyle="{StaticResource NonSelectableDataGridCellStyle}"
    SelectionUnit="FullRow" 
    HorizontalContentAlignment="Stretch" AutoGenerateColumns="False">
21
Omar Mir

これは動作します:

MultiSelectorBehaviours.vb

Imports System.Collections
Imports System.Windows
Imports System.Windows.Controls.Primitives
Imports System.Windows.Controls
Imports System

Public NotInheritable Class MultiSelectorBehaviours
    Private Sub New()
    End Sub

    Public Shared ReadOnly SynchronizedSelectedItems As DependencyProperty = _
        DependencyProperty.RegisterAttached("SynchronizedSelectedItems", GetType(IList), GetType(MultiSelectorBehaviours), New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnSynchronizedSelectedItemsChanged)))

    Private Shared ReadOnly SynchronizationManagerProperty As DependencyProperty = DependencyProperty.RegisterAttached("SynchronizationManager", GetType(SynchronizationManager), GetType(MultiSelectorBehaviours), New PropertyMetadata(Nothing))

    ''' <summary>
    ''' Gets the synchronized selected items.
    ''' </summary>
    ''' <param name="dependencyObject">The dependency object.</param>
    ''' <returns>The list that is acting as the sync list.</returns>
    Public Shared Function GetSynchronizedSelectedItems(ByVal dependencyObject As DependencyObject) As IList
        Return DirectCast(dependencyObject.GetValue(SynchronizedSelectedItems), IList)
    End Function

    ''' <summary>
    ''' Sets the synchronized selected items.
    ''' </summary>
    ''' <param name="dependencyObject">The dependency object.</param>
    ''' <param name="value">The value to be set as synchronized items.</param>
    Public Shared Sub SetSynchronizedSelectedItems(ByVal dependencyObject As DependencyObject, ByVal value As IList)
        dependencyObject.SetValue(SynchronizedSelectedItems, value)
    End Sub

    Private Shared Function GetSynchronizationManager(ByVal dependencyObject As DependencyObject) As SynchronizationManager
        Return DirectCast(dependencyObject.GetValue(SynchronizationManagerProperty), SynchronizationManager)
    End Function

    Private Shared Sub SetSynchronizationManager(ByVal dependencyObject As DependencyObject, ByVal value As SynchronizationManager)
        dependencyObject.SetValue(SynchronizationManagerProperty, value)
    End Sub

    Private Shared Sub OnSynchronizedSelectedItemsChanged(ByVal dependencyObject As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        If e.OldValue IsNot Nothing Then
            Dim synchronizer As SynchronizationManager = GetSynchronizationManager(dependencyObject)
            synchronizer.StopSynchronizing()

            SetSynchronizationManager(dependencyObject, Nothing)
        End If

        Dim list As IList = TryCast(e.NewValue, IList)
        Dim selector As Selector = TryCast(dependencyObject, Selector)

        ' check that this property is an IList, and that it is being set on a ListBox
        If list IsNot Nothing AndAlso selector IsNot Nothing Then
            Dim synchronizer As SynchronizationManager = GetSynchronizationManager(dependencyObject)
            If synchronizer Is Nothing Then
                synchronizer = New SynchronizationManager(selector)
                SetSynchronizationManager(dependencyObject, synchronizer)
            End If

            synchronizer.StartSynchronizingList()
        End If
    End Sub

    ''' <summary>
    ''' A synchronization manager.
    ''' </summary>
    Private Class SynchronizationManager
        Private ReadOnly _multiSelector As Selector
        Private _synchronizer As TwoListSynchronizer

        ''' <summary>
        ''' Initializes a new instance of the <see cref="SynchronizationManager"/> class.
        ''' </summary>
        ''' <param name="selector">The selector.</param>
        Friend Sub New(ByVal selector As Selector)
            _multiSelector = selector
        End Sub

        ''' <summary>
        ''' Starts synchronizing the list.
        ''' </summary>
        Public Sub StartSynchronizingList()
            Dim list As IList = GetSynchronizedSelectedItems(_multiSelector)

            If list IsNot Nothing Then
                _synchronizer = New TwoListSynchronizer(GetSelectedItemsCollection(_multiSelector), list)
                _synchronizer.StartSynchronizing()
            End If
        End Sub

        ''' <summary>
        ''' Stops synchronizing the list.
        ''' </summary>
        Public Sub StopSynchronizing()
            _synchronizer.StopSynchronizing()
        End Sub

        Public Shared Function GetSelectedItemsCollection(ByVal selector As Selector) As IList
            If TypeOf selector Is MultiSelector Then
                Return TryCast(selector, MultiSelector).SelectedItems
            ElseIf TypeOf selector Is ListBox Then
                Return TryCast(selector, ListBox).SelectedItems
            Else
                Throw New InvalidOperationException("Target object has no SelectedItems property to bind.")
            End If
        End Function

    End Class
End Class

IListItemConverter.vb

''' <summary>
''' Converts items in the Master list to Items in the target list, and back again.
''' </summary>
Public Interface IListItemConverter
    ''' <summary>
    ''' Converts the specified master list item.
    ''' </summary>
    ''' <param name="masterListItem">The master list item.</param>
    ''' <returns>The result of the conversion.</returns>
    Function Convert(ByVal masterListItem As Object) As Object

    ''' <summary>
    ''' Converts the specified target list item.
    ''' </summary>
    ''' <param name="targetListItem">The target list item.</param>
    ''' <returns>The result of the conversion.</returns>
    Function ConvertBack(ByVal targetListItem As Object) As Object
End Interface

TwoListSynchronizer.vb

Imports System.Collections
Imports System.Collections.Specialized
Imports System.Linq
Imports System.Windows

''' <summary>
''' Keeps two lists synchronized. 
''' </summary>
Public Class TwoListSynchronizer
    Implements IWeakEventListener

    Private Shared ReadOnly DefaultConverter As IListItemConverter = New DoNothingListItemConverter()
    Private ReadOnly _masterList As IList
    Private ReadOnly _masterTargetConverter As IListItemConverter
    Private ReadOnly _targetList As IList


    ''' <summary>
    ''' Initializes a new instance of the <see cref="TwoListSynchronizer"/> class.
    ''' </summary>
    ''' <param name="masterList">The master list.</param>
    ''' <param name="targetList">The target list.</param>
    ''' <param name="masterTargetConverter">The master-target converter.</param>
    Public Sub New(ByVal masterList As IList, ByVal targetList As IList, ByVal masterTargetConverter As IListItemConverter)
        _masterList = masterList
        _targetList = targetList
        _masterTargetConverter = masterTargetConverter
    End Sub

    ''' <summary>
    ''' Initializes a new instance of the <see cref="TwoListSynchronizer"/> class.
    ''' </summary>
    ''' <param name="masterList">The master list.</param>
    ''' <param name="targetList">The target list.</param>
    Public Sub New(ByVal masterList As IList, ByVal targetList As IList)
        Me.New(masterList, targetList, DefaultConverter)
    End Sub

    Private Delegate Sub ChangeListAction(ByVal list As IList, ByVal e As NotifyCollectionChangedEventArgs, ByVal converter As Converter(Of Object, Object))

    ''' <summary>
    ''' Starts synchronizing the lists.
    ''' </summary>
    Public Sub StartSynchronizing()
        ListenForChangeEvents(_masterList)
        ListenForChangeEvents(_targetList)

        ' Update the Target list from the Master list
        SetListValuesFromSource(_masterList, _targetList, AddressOf ConvertFromMasterToTarget)

        ' In some cases the target list might have its own view on which items should included:
        ' so update the master list from the target list
        ' (This is the case with a ListBox SelectedItems collection: only items from the ItemsSource can be included in SelectedItems)
        If Not TargetAndMasterCollectionsAreEqual() Then
            SetListValuesFromSource(_targetList, _masterList, AddressOf ConvertFromTargetToMaster)
        End If
    End Sub

    ''' <summary>
    ''' Stop synchronizing the lists.
    ''' </summary>
    Public Sub StopSynchronizing()
        StopListeningForChangeEvents(_masterList)
        StopListeningForChangeEvents(_targetList)
    End Sub

    ''' <summary>
    ''' Receives events from the centralized event manager.
    ''' </summary>
    ''' <param name="managerType">The type of the <see cref="T:System.Windows.WeakEventManager"/> calling this method.</param>
    ''' <param name="sender">Object that originated the event.</param>
    ''' <param name="e">Event data.</param>
    ''' <returns>
    ''' true if the listener handled the event. It is considered an error by the <see cref="T:System.Windows.WeakEventManager"/> handling in WPF to register a listener for an event that the listener does not handle. Regardless, the method should return false if it receives an event that it does not recognize or handle.
    ''' </returns>
    Public Function ReceiveWeakEvent(ByVal managerType As Type, ByVal sender As Object, ByVal e As EventArgs) As Boolean Implements System.Windows.IWeakEventListener.ReceiveWeakEvent
        HandleCollectionChanged(TryCast(sender, IList), TryCast(e, NotifyCollectionChangedEventArgs))

        Return True
    End Function

    ''' <summary>
    ''' Listens for change events on a list.
    ''' </summary>
    ''' <param name="list">The list to listen to.</param>
    Protected Sub ListenForChangeEvents(ByVal list As IList)
        If TypeOf list Is INotifyCollectionChanged Then
            CollectionChangedEventManager.AddListener(TryCast(list, INotifyCollectionChanged), Me)
        End If
    End Sub

    ''' <summary>
    ''' Stops listening for change events.
    ''' </summary>
    ''' <param name="list">The list to stop listening to.</param>
    Protected Sub StopListeningForChangeEvents(ByVal list As IList)
        If TypeOf list Is INotifyCollectionChanged Then
            CollectionChangedEventManager.RemoveListener(TryCast(list, INotifyCollectionChanged), Me)
        End If
    End Sub

    Private Sub AddItems(ByVal list As IList, ByVal e As NotifyCollectionChangedEventArgs, ByVal converter As Converter(Of Object, Object))
        Dim itemCount As Integer = e.NewItems.Count

        For i As Integer = 0 To itemCount - 1
            Dim insertionPoint As Integer = e.NewStartingIndex + i

            If insertionPoint > list.Count Then
                list.Add(converter(e.NewItems(i)))
            Else
                list.Insert(insertionPoint, converter(e.NewItems(i)))
            End If
        Next
    End Sub

    Private Function ConvertFromMasterToTarget(ByVal masterListItem As Object) As Object
        Return If(_masterTargetConverter Is Nothing, masterListItem, _masterTargetConverter.Convert(masterListItem))
    End Function

    Private Function ConvertFromTargetToMaster(ByVal targetListItem As Object) As Object
        Return If(_masterTargetConverter Is Nothing, targetListItem, _masterTargetConverter.ConvertBack(targetListItem))
    End Function

    Private Sub HandleCollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
        Dim sourceList As IList = TryCast(sender, IList)

        Select Case e.Action
            Case NotifyCollectionChangedAction.Add
                PerformActionOnAllLists(AddressOf AddItems, sourceList, e)
                Exit Select
            Case NotifyCollectionChangedAction.Move
                PerformActionOnAllLists(AddressOf MoveItems, sourceList, e)
                Exit Select
            Case NotifyCollectionChangedAction.Remove
                PerformActionOnAllLists(AddressOf RemoveItems, sourceList, e)
                Exit Select
            Case NotifyCollectionChangedAction.Replace
                PerformActionOnAllLists(AddressOf ReplaceItems, sourceList, e)
                Exit Select
            Case NotifyCollectionChangedAction.Reset
                UpdateListsFromSource(TryCast(sender, IList))
                Exit Select
            Case Else
                Exit Select
        End Select
    End Sub

    Private Sub MoveItems(ByVal list As IList, ByVal e As NotifyCollectionChangedEventArgs, ByVal converter As Converter(Of Object, Object))
        RemoveItems(list, e, converter)
        AddItems(list, e, converter)
    End Sub

    Private Sub PerformActionOnAllLists(ByVal action As ChangeListAction, ByVal sourceList As IList, ByVal collectionChangedArgs As NotifyCollectionChangedEventArgs)
        If sourceList Is _masterList Then
            PerformActionOnList(_targetList, action, collectionChangedArgs, AddressOf ConvertFromMasterToTarget)
        Else
            PerformActionOnList(_masterList, action, collectionChangedArgs, AddressOf ConvertFromTargetToMaster)
        End If
    End Sub

    Private Sub PerformActionOnList(ByVal list As IList, ByVal action As ChangeListAction, ByVal collectionChangedArgs As NotifyCollectionChangedEventArgs, ByVal converter As Converter(Of Object, Object))
        StopListeningForChangeEvents(list)
        action(list, collectionChangedArgs, converter)
        ListenForChangeEvents(list)
    End Sub

    Private Sub RemoveItems(ByVal list As IList, ByVal e As NotifyCollectionChangedEventArgs, ByVal converter As Converter(Of Object, Object))
        Dim itemCount As Integer = e.OldItems.Count

        ' for the number of items being removed, remove the item from the Old Starting Index
        ' (this will cause following items to be shifted down to fill the hole).
        For i As Integer = 0 To itemCount - 1
            list.RemoveAt(e.OldStartingIndex)
        Next
    End Sub

    Private Sub ReplaceItems(ByVal list As IList, ByVal e As NotifyCollectionChangedEventArgs, ByVal converter As Converter(Of Object, Object))
        RemoveItems(list, e, converter)
        AddItems(list, e, converter)
    End Sub

    Private Sub SetListValuesFromSource(ByVal sourceList As IList, ByVal targetList As IList, ByVal converter As Converter(Of Object, Object))
        StopListeningForChangeEvents(targetList)

        targetList.Clear()

        For Each o As Object In sourceList
            targetList.Add(converter(o))
        Next

        ListenForChangeEvents(targetList)
    End Sub

    Private Function TargetAndMasterCollectionsAreEqual() As Boolean
        Return _masterList.Cast(Of Object)().SequenceEqual(_targetList.Cast(Of Object)().[Select](Function(item) ConvertFromTargetToMaster(item)))
    End Function

    ''' <summary>
    ''' Makes sure that all synchronized lists have the same values as the source list.
    ''' </summary>
    ''' <param name="sourceList">The source list.</param>
    Private Sub UpdateListsFromSource(ByVal sourceList As IList)
        If sourceList Is _masterList Then
            SetListValuesFromSource(_masterList, _targetList, AddressOf ConvertFromMasterToTarget)
        Else
            SetListValuesFromSource(_targetList, _masterList, AddressOf ConvertFromTargetToMaster)
        End If
    End Sub




    ''' <summary>
    ''' An implementation that does nothing in the conversions.
    ''' </summary>
    Friend Class DoNothingListItemConverter
        Implements IListItemConverter

        ''' <summary>
        ''' Converts the specified master list item.
        ''' </summary>
        ''' <param name="masterListItem">The master list item.</param>
        ''' <returns>The result of the conversion.</returns>
        Public Function Convert(ByVal masterListItem As Object) As Object Implements IListItemConverter.Convert
            Return masterListItem
        End Function

        ''' <summary>
        ''' Converts the specified target list item.
        ''' </summary>
        ''' <param name="targetListItem">The target list item.</param>
        ''' <returns>The result of the conversion.</returns>
        Public Function ConvertBack(ByVal targetListItem As Object) As Object Implements IListItemConverter.ConvertBack
            Return targetListItem
        End Function
    End Class

End Class

XAMLの場合:

<DataGrid ..... local:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedResults}" />

そして最後にVM:

Public ReadOnly Property SelectedResults As ObservableCollection(Of StatisticsResultModel)
    Get
        Return _objSelectedResults
    End Get
End Property

クレジットの移動先: http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html

3
Omar Mir

私はあなたを保証することができます:SelectedItemsは確かにXAMLとしてバインド可能ですCommandParameter

多くの掘り下げとグーグルの後で、私はこの一般的な問題の簡単な解決策をついに見つけました。

これを機能させるには、次のすべてのルールに従ってください

  1. Ed Ballの提案 'に従って、XAMLコマンドデータバインディングでCommandParameter property BEFORE Command propertyを定義します。これは非常に時間がかかるバグです。

    enter image description here

  2. ICommandCanExecuteおよびExecuteメソッドにobjectタイプのパラメーターがあることを確認してください。このようにして、データバインディングCommandParameterタイプがコマンドメソッドのパラメータータイプと一致しない場合に必ず発生するsilencedキャスト例外を防ぐことができます。

    private bool OnDeleteSelectedItemsCanExecute(object SelectedItems)  
    {
        // Your code goes here
    }
    
    private bool OnDeleteSelectedItemsExecute(object SelectedItems)  
    {
        // Your code goes here
    }
    

たとえば、リストビュー/リストボックスのSelectedItemsプロパティをICommandメソッドに送信するか、リストビュー/リストボックス自体を送信できます。すごいですね。

SelectedItemsCanExecuteパラメータとして受け取る方法を理解するために誰かが膨大な時間を費やさないようにしてください。

47
Julio Nobre

私の元の答えは間違っていました(読み取り専用プロパティであるため、SelectedItemsにバインドできません)。

これを回避するかなりMVVMフレンドリーな方法の1つは、IsSelectedDataGridRowプロパティにバインドすることです。

次のようにバインディングを設定できます。

_<DataGrid ItemsSource="{Binding DocumentViewModels}"
          SelectionMode="Extended">
    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <Setter Property="IsSelected"
                    Value="{Binding IsSelected}" />
        </Style>
    </DataGrid.Resources>
</DataGrid>
_

次に、DocumentViewModel(または使用しているMVVM基本クラス)から継承し、DataGridに表示するViewModelBaseのプロパティを持つDocumentを作成する必要があります。 、およびIsSelectedプロパティ。

次に、メインビューモデルで、DocumentViewModelsをバインドするDataGridというList(Of DocumentViewModel)を作成します。 (注:リストから項目を追加/削除する場合は、代わりにObservableCollection(T)を使用してください。)

さて、ここがトリッキーな部分です。次のように、リスト内の各PropertyChangedDocumentViewModelイベントにフックする必要があります。

_For Each documentViewModel As DocumentViewModel In DocumentViewModels
    documentViewModel.PropertyChanged += DocumentViewModel_PropertyChanged
Next
_

これにより、DocumentViewModelの変更に対応できます。

最後に、_DocumentViewModel_PropertyChanged_では、リストをループして(またはLinqクエリを使用して)、_IsSelected = True_である各アイテムの情報を取得できます。

24
devuxer

ちょっとしたトリックで、DataGridを拡張して、SelectedItemsプロパティのバインド可能なバージョンを作成できます。私のソリューションでは、バインディングにMode=OneWayToSourceとにかくプロパティから読み取るだけなので、可能性があるソリューションを拡張して、プロパティを読み書きできるようにすることができます。

ListBoxにも同様の手法が使用できると思いますが、まだ試していません。

public class BindableMultiSelectDataGrid : DataGrid
{
    public static readonly DependencyProperty SelectedItemsProperty =
        DependencyProperty.Register("SelectedItems", typeof(IList), typeof(BindableMultiSelectDataGrid), new PropertyMetadata(default(IList)));

    public new IList SelectedItems
    {
        get { return (IList)GetValue(SelectedItemsProperty); }
        set { throw new Exception("This property is read-only. To bind to it you must use 'Mode=OneWayToSource'."); }
    }

    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        base.OnSelectionChanged(e);
        SetValue(SelectedItemsProperty, base.SelectedItems);
    }
}
9
Brian Hinchey

私は答えのためにここに来ました、そして私はたくさんの素晴らしいものを得ました。これらすべてを組み合わせて、上記のOmarが提供するプロパティと非常に似ていますが、1つのクラスの添付プロパティにまとめました。 INotifyCollectionChangedを処理し、リストを切り替えます。イベントをリークしません。私はそれを書いたので、それに従うのはかなり単純なコードになるでしょう。 C#で記述され、リストボックスselectedItemsおよびdataGrid selectedItemsを処理します。

これは、DataGridとListBoxの両方で機能します。

(私はGitHubの使い方を学びました)GitHub https://github.com/ParrhesiaJoe/SelectedItemsAttachedWpf

使用するには:

<ListBox ItemsSource="{Binding MyList}" a:Ex.SelectedItems="{Binding ObservableList}" 
         SelectionMode="Extended"/>
<DataGrid ItemsSource="{Binding MyList}" a:Ex.SelectedItems="{Binding OtherObservableList}" />

そして、これがコードです。 Gitに小さなサンプルがあります。

public class Ex : DependencyObject
{
    public static readonly DependencyProperty IsSubscribedToSelectionChangedProperty = DependencyProperty.RegisterAttached(
        "IsSubscribedToSelectionChanged", typeof(bool), typeof(Ex), new PropertyMetadata(default(bool)));
    public static void SetIsSubscribedToSelectionChanged(DependencyObject element, bool value) { element.SetValue(IsSubscribedToSelectionChangedProperty, value); }
    public static bool GetIsSubscribedToSelectionChanged(DependencyObject element) { return (bool)element.GetValue(IsSubscribedToSelectionChangedProperty); }

    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached(
        "SelectedItems", typeof(IList), typeof(Ex), new PropertyMetadata(default(IList), OnSelectedItemsChanged));
    public static void SetSelectedItems(DependencyObject element, IList value) { element.SetValue(SelectedItemsProperty, value); }
    public static IList GetSelectedItems(DependencyObject element) { return (IList)element.GetValue(SelectedItemsProperty); }

    /// <summary>
    /// Attaches a list or observable collection to the grid or listbox, syncing both lists (one way sync for simple lists).
    /// </summary>
    /// <param name="d">The DataGrid or ListBox</param>
    /// <param name="e">The list to sync to.</param>
    private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (!(d is ListBox || d is MultiSelector))
            throw new ArgumentException("Somehow this got attached to an object I don't support. ListBoxes and Multiselectors (DataGrid), people. Geesh =P!");

        var selector = (Selector)d;
        var oldList = e.OldValue as IList;
        if (oldList != null)
        {
            var obs = oldList as INotifyCollectionChanged;
            if (obs != null)
            {
                obs.CollectionChanged -= OnCollectionChanged;
            }
            // If we're orphaned, disconnect lb/dg events.
            if (e.NewValue == null)
            {
                selector.SelectionChanged -= OnSelectorSelectionChanged;
                SetIsSubscribedToSelectionChanged(selector, false);
            }
        }
        var newList = (IList)e.NewValue;
        if (newList != null)
        {
            var obs = newList as INotifyCollectionChanged;
            if (obs != null)
            {
                obs.CollectionChanged += OnCollectionChanged;
            }
            PushCollectionDataToSelectedItems(newList, selector);
            var isSubscribed = GetIsSubscribedToSelectionChanged(selector);
            if (!isSubscribed)
            {
                selector.SelectionChanged += OnSelectorSelectionChanged;
                SetIsSubscribedToSelectionChanged(selector, true);
            }
        }
    }

    /// <summary>
    /// Initially set the selected items to the items in the newly connected collection,
    /// unless the new collection has no selected items and the listbox/grid does, in which case
    /// the flow is reversed. The data holder sets the state. If both sides hold data, then the
    /// bound IList wins and dominates the helpless wpf control.
    /// </summary>
    /// <param name="obs">The list to sync to</param>
    /// <param name="selector">The grid or listbox</param>
    private static void PushCollectionDataToSelectedItems(IList obs, DependencyObject selector)
    {
        var listBox = selector as ListBox;
        if (listBox != null)
        {
            if (obs.Count > 0)
            {
                listBox.SelectedItems.Clear();
                foreach (var ob in obs) { listBox.SelectedItems.Add(ob); }
            }
            else
            {
                foreach (var ob in listBox.SelectedItems) { obs.Add(ob); }
            }
            return;
        }
        // Maybe other things will use the multiselector base... who knows =P
        var grid = selector as MultiSelector;
        if (grid != null)
        {
            if (obs.Count > 0)
            {
                grid.SelectedItems.Clear();
                foreach (var ob in obs) { grid.SelectedItems.Add(ob); }
            }
            else
            {
                foreach (var ob in grid.SelectedItems) { obs.Add(ob); }
            }
            return;
        }
        throw new ArgumentException("Somehow this got attached to an object I don't support. ListBoxes and Multiselectors (DataGrid), people. Geesh =P!");
    }
    /// <summary>
    /// When the listbox or grid fires a selectionChanged even, we update the attached list to
    /// match it.
    /// </summary>
    /// <param name="sender">The listbox or grid</param>
    /// <param name="e">Items added and removed.</param>
    private static void OnSelectorSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var dep = (DependencyObject)sender;
        var items = GetSelectedItems(dep);
        var col = items as INotifyCollectionChanged;

        // Remove the events so we don't fire back and forth, then re-add them.
        if (col != null) col.CollectionChanged -= OnCollectionChanged;
        foreach (var oldItem in e.RemovedItems) items.Remove(oldItem);
        foreach (var newItem in e.AddedItems) items.Add(newItem);
        if (col != null) col.CollectionChanged += OnCollectionChanged;
    }

    /// <summary>
    /// When the attached object implements INotifyCollectionChanged, the attached listbox
    /// or grid will have its selectedItems adjusted by this handler.
    /// </summary>
    /// <param name="sender">The listbox or grid</param>
    /// <param name="e">The added and removed items</param>
    private static void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // Push the changes to the selected item.
        var listbox = sender as ListBox;
        if (listbox != null)
        {
            listbox.SelectionChanged -= OnSelectorSelectionChanged;
            if (e.Action == NotifyCollectionChangedAction.Reset) listbox.SelectedItems.Clear();
            else
            {
                foreach (var oldItem in e.OldItems) listbox.SelectedItems.Remove(oldItem);
                foreach (var newItem in e.NewItems) listbox.SelectedItems.Add(newItem);
            }
            listbox.SelectionChanged += OnSelectorSelectionChanged;
        }
        var grid = sender as MultiSelector;
        if (grid != null)
        {
            grid.SelectionChanged -= OnSelectorSelectionChanged;
            if (e.Action == NotifyCollectionChangedAction.Reset) grid.SelectedItems.Clear();
            else
            {
                foreach (var oldItem in e.OldItems) grid.SelectedItems.Remove(oldItem);
                foreach (var newItem in e.NewItems) grid.SelectedItems.Add(newItem);
            }
            grid.SelectionChanged += OnSelectorSelectionChanged;
        }
    }
 }
6
Parrhesia Joe

この投稿は少し古く、回答済みです。しかし、MVVM以外の答えを思いつきました。その簡単で、私のために働く。 Selected CollectionがSelectedResultsであると想定して、別のDataGridを追加します。

    <DataGrid x:Name="SelectedGridRows" 
        ItemsSource="{Binding SelectedResults,Mode=OneWayToSource}" 
        Visibility="Collapsed" >

そして、コードビハインドで、これをコンストラクターに追加します。

    public ClassConstructor()
    {
        InitializeComponent();
        OriginalGrid.SelectionChanged -= OriginalGrid_SelectionChanged;
        OriginalGrid.SelectionChanged += OriginalGrid_SelectionChanged;
    }
    private void OriginalGrid_SelectionChanged(object sender, 
        SelectionChangedEventArgs e)
    {
        SelectedGridRows.ItemsSource = OriginalGrid.SelectedItems;
    }

これが簡単な解決策の1つです。このようにして、データをViewModelに渡したり更新したりできます。

Designer.xaml

<DataGrid Grid.Row="1" Name="dgvMain" SelectionChanged="DataGrid_SelectionChanged" />

Designer.cs

ViewModel mModel = null;
public Designer()
{
    InitializeComponent();

    mModel = new ViewModel();
    this.DataContext = mModel;
}

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    mModel.SelectedItems = dgvMain.SelectedItems;
}

ViewModel.cs

public class ViewModel
{
    public IList SelectedItems { get; set; }
}
4
Krishna

私のニーズに合った回避策を使用してこれに対する解決策があります。

EventToCommandListItemTemplateMouseUpを作成し、CommandParameterとしてSelectedItemsコレクションを送信します

 <i:Interaction.Triggers>
     <i:EventTrigger EventName="MouseUp">
         <helpers:EventToCommand Command="{Binding DataContext.SelectionChangedUpdate,
                                 RelativeSource={RelativeSource AncestorType=UserControl}}"
                                 CommandParameter="{Binding ElementName=personsList, Path=SelectedItems}" />
    </i:EventTrigger>                         
</i:Interaction.Triggers>

そうすることで、これを処理するか、選択したアイテムを後で使用するために保存するコマンドをビューモデルに含めることができます。コーディングを楽しんでください

4
Mihai

私にとって最も簡単な方法は、SelectionChangedイベントにViewModelプロパティを設定することでした。

private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    (DataContext as MyViewModel).SelectedItems.Clear();
    foreach (var i in MyDataGrid.SelectedItems) (DataContext as MyViewModel).SelectedItems.Add(i as ItemType);
}
3
Neil B

コンバーターの使用についてはどうですか?

public class SelectedItemsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dg = value as DataGrid;
        return dg?.SelectedItems;
    }
...

次のようにDataGridContextMenuで使用します。

       <DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem
                    CommandParameter="{Binding Path=MySelectedItems, Converter={StaticResource selectedItemsConverter}, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
                    Command="{Binding MyDoSomethingWithMySelectedItemsCommand}"
                    Header="Do Something...">
                </MenuItem>
0

直接バインドしてビューモデルを行う、少しトリッキーなバージョン:

1)ICommandを作成します。

public class GetSelectedItemsCommand : ICommand
{
    public GetSelectedItemsCommand(Action<object> action)
    {
        _action = action;
    }

    private readonly Action<object> _action;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _action(parameter);
    }
}

2)DataGridを作成する

<DataGrid x:Name="DataGridOfDesperatePeople" SelectionMode="Extended">
    <i:Interaction.Triggers>
         <i:EventTrigger EventName="SelectionChanged">
              <i:InvokeCommandAction CommandParameter="{Binding ElementName=DataGridOfDesperatePeople, Path=SelectedItems}" Command="{Binding SelectedItemsCommand }" />
          </i:EventTrigger>
    </i:Interaction.Triggers>
</DataGrid>

3)ビューモデルで作成

public List<YourClassOfItemInTheGrid> SelectedItems { get; set; }  = new List<YourClassOfItemInTheGrid>();

public ICommand SelectedItemsCommand
{
    get
    {
        return new GetSelectedItemsCommand(list =>
        {

            SelectedItems.Clear();
            IList items = (IList)list;
            IEnumerable<YourClassOfItemInTheGrid> collection = items.Cast<YourClassOfItemInTheGrid>();
            SelectedItems = collection.ToList();
        });
    }
}
0
Marek Schwarz