我在MVVM模式上开发了UI,现在停留在获取SelectedItems上。您能否修改我的XAML并提供示例如何在insde ViewModel类中获取它们。

<xcdg:DataGridControl Name="ResultGrid" ItemsSource="{Binding Results}"  Height="295" HorizontalAlignment="Left" Margin="6,25,0,0" VerticalAlignment="Top" Width="1041" ReadOnly="True">
                    <xcdg:DataGridControl.View>
                        <xcdg:TableflowView UseDefaultHeadersFooters="False">
                            <xcdg:TableflowView.FixedHeaders>
                                <DataTemplate>
                                    <xcdg:ColumnManagerRow />
                                </DataTemplate>
                            </xcdg:TableflowView.FixedHeaders>
                        </xcdg:TableflowView>
                    </xcdg:DataGridControl.View>
                </xcdg:DataGridControl>

最佳答案

您可以使用附加行为来将SelectedItems获取/设置为datagrid。

我在Metro应用程序中遇到了类似的问题,因此必须自己编写。

以下是链接

http://www.codeproject.com/Articles/412417/Managing-Multiple-selection-in-View-Model-NET-Metr

尽管我已经为Metro应用程序撰写过文章,但是相同的解决方案可以在WPF/Silverlight中进行调整。

    public class MultiSelectBehavior : Behavior<ListViewBase>
        {
            #region SelectedItems Attached Property
            public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(
                "SelectedItems",
                typeof(ObservableCollection<object>),
                typeof(MultiSelectBehavior),
                new PropertyMetadata(new ObservableCollection<object>(), PropertyChangedCallback));

            #endregion

            #region private
            private bool _selectionChangedInProgress; // Flag to avoid infinite loop if same viewmodel is shared by multiple controls
            #endregion

            public MultiSelectBehavior()
            {
                SelectedItems = new ObservableCollection<object>();
            }

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

            protected override void OnAttached()
            {
                base.OnAttached();
                AssociatedObject.SelectionChanged += OnSelectionChanged;
            }

            protected override void OnDetaching()
            {
                base.OnDetaching();
                AssociatedObject.SelectionChanged -= OnSelectionChanged;
            }

            private static void PropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                NotifyCollectionChangedEventHandler handler =  (s, e) => SelectedItemsChanged(sender, e);
                if (args.OldValue is ObservableCollection<object>)
                {
                    (args.OldValue as ObservableCollection<object>).CollectionChanged -= handler;
                }

                if (args.NewValue is ObservableCollection<object>)
                {
                    (args.NewValue as ObservableCollection<object>).CollectionChanged += handler;
                }
            }

            private static void SelectedItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
            {
                if (sender is MultiSelectBehavior)
                {
                    var listViewBase = (sender as MultiSelectBehavior).AssociatedObject;

                    var listSelectedItems = listViewBase.SelectedItems;
                    if (e.OldItems != null)
                    {
                        foreach (var item in e.OldItems)
                        {
                            if (listSelectedItems.Contains(item))
                            {
                                listSelectedItems.Remove(item);
                            }
                        }
                    }

                    if (e.NewItems != null)
                    {
                        foreach (var item in e.NewItems)
                        {
                            if (!listSelectedItems.Contains(item))
                            {
                                listSelectedItems.Add(item);
                            }
                        }
                    }
                }
            }

            private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (_selectionChangedInProgress) return;
                _selectionChangedInProgress = true;
                foreach (var item in e.RemovedItems)
                {
                    if (SelectedItems.Contains(item))
                    {
                        SelectedItems.Remove(item);
                    }
                }

                foreach (var item in e.AddedItems)
                {
                    if (!SelectedItems.Contains(item))
                    {
                        SelectedItems.Add(item);
                    }
                }
                _selectionChangedInProgress = false;
            }
        }

关于c# - DataGrid.SelectedItems MVVM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11332244/

10-13 07:08