WPF M-V-VM:从ListCollectionView中获取所选项目?

时间:2009-01-16 20:00:09

标签: wpf mvvm listbox

我有一个使用Model-View-ViewModel模式的WPF应用程序 在我的ViewModel中,我有一个ListCollectionView来保存项目列表 此ListCollectionView绑定到我的View中的ListBox。

<ListBox Grid.Row="1" ItemsSource="{Binding Useragents}" SelectionMode="Multiple"/>

ListBox的SelectionMode = Multiple,因此您可以一次选择更多项目。现在,ViewModel需要知道选择了哪些项目。

问题是:在View-Model-ViewModel模式中,ViewModel无法访问View,因此我不能只询问ListBox选择了哪些项目。我只有ListCollectionView,但我找不到一种方法来找到那里选择的项目。

那么如何在ListBox中找到已选择的项目?或者实现这一目标的技巧(可能在我的项目中绑定某个布尔'IsSelected'但是什么?怎么样?)

也许有人使用这种模式也可以帮助我吗?

9 个答案:

答案 0 :(得分:12)

您需要在其上创建一个具有IsSelected概念的ViewModel,并绑定到使用标准WPF绑定体系结构在View中表示它的实际ListBoxItem的IsSelected属性。

然后在您的代码中,它知道您的ViewModel,而不是它由任何特定View表示的事实,可以使用该属性来找出模型中的哪些项目实际被选中,而不管设计者如何选择它在视图中表示。

答案 1 :(得分:8)

PRISM MVVM Reference Implementation有一个名为SynchronizeSelectedItems的行为,在Prism4 \ MVVM RI \ MVVM.Client \ Views \ MultipleSelectionView.xaml中使用,它将已检查项与名为Selections的ViewModel属性同步:

        <ListBox Grid.Column="0" Grid.Row="1" IsTabStop="False" SelectionMode="Multiple"
                 ItemsSource="{Binding Question.Range}" Margin="5">

            <ListBox.ItemContainerStyle>
                <!-- Custom style to show the multi-selection list box as a collection of check boxes -->
                <Style TargetType="ListBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Grid Background="Transparent">
                                    <CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" 
                                              IsHitTestVisible="False" IsTabStop="True"
                                              AutomationProperties.AutomationId="CheckBoxAutomationId">
                                        <ContentPresenter/>
                                    </CheckBox>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <i:Interaction.Behaviors>
                <!-- Custom behavior that synchronizes the selected items with the view models collection -->
                <Behaviors:SynchronizeSelectedItems Selections="{Binding Selections}"/>
            </i:Interaction.Behaviors>
        </ListBox>

转到http://compositewpf.codeplex.com/并全部抓取或使用:

//===================================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation and Silverlight
//===================================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===================================================================================
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace MVVM.Client.Infrastructure.Behaviors
{
    /// <summary>
    /// Custom behavior that synchronizes the list in <see cref="ListBox.SelectedItems"/> with a collection.
    /// </summary>
    /// <remarks>
    /// This behavior uses a weak event handler to listen for changes on the synchronized collection.
    /// </remarks>
    public class SynchronizeSelectedItems : Behavior<ListBox>
    {
        public static readonly DependencyProperty SelectionsProperty =
            DependencyProperty.Register(
                "Selections",
                typeof(IList),
                typeof(SynchronizeSelectedItems),
                new PropertyMetadata(null, OnSelectionsPropertyChanged));

        private bool updating;
        private WeakEventHandler<SynchronizeSelectedItems, object, NotifyCollectionChangedEventArgs> currentWeakHandler;

        [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",
            Justification = "Dependency property")]
        public IList Selections
        {
            get { return (IList)this.GetValue(SelectionsProperty); }
            set { this.SetValue(SelectionsProperty, value); }
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            this.AssociatedObject.SelectionChanged += this.OnSelectedItemsChanged;
            this.UpdateSelectedItems();
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.SelectionChanged += this.OnSelectedItemsChanged;

            base.OnDetaching();
        }

        private static void OnSelectionsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var behavior = d as SynchronizeSelectedItems;

            if (behavior != null)
            {
                if (behavior.currentWeakHandler != null)
                {
                    behavior.currentWeakHandler.Detach();
                    behavior.currentWeakHandler = null;
                }

                if (e.NewValue != null)
                {
                    var notifyCollectionChanged = e.NewValue as INotifyCollectionChanged;
                    if (notifyCollectionChanged != null)
                    {
                        behavior.currentWeakHandler =
                            new WeakEventHandler<SynchronizeSelectedItems, object, NotifyCollectionChangedEventArgs>(
                                behavior,
                                (instance, sender, args) => instance.OnSelectionsCollectionChanged(sender, args),
                                (listener) => notifyCollectionChanged.CollectionChanged -= listener.OnEvent);
                        notifyCollectionChanged.CollectionChanged += behavior.currentWeakHandler.OnEvent;
                    }

                    behavior.UpdateSelectedItems();
                }
            }
        }

        private void OnSelectedItemsChanged(object sender, SelectionChangedEventArgs e)
        {
            this.UpdateSelections(e);
        }

        private void UpdateSelections(SelectionChangedEventArgs e)
        {
            this.ExecuteIfNotUpdating(
                () =>
                {
                    if (this.Selections != null)
                    {
                        foreach (var item in e.AddedItems)
                        {
                            this.Selections.Add(item);
                        }

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

        private void OnSelectionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.UpdateSelectedItems();
        }

        private void UpdateSelectedItems()
        {
            this.ExecuteIfNotUpdating(
                () =>
                {
                    if (this.AssociatedObject != null)
                    {
                        this.AssociatedObject.SelectedItems.Clear();
                        foreach (var item in this.Selections ?? new object[0])
                        {
                            this.AssociatedObject.SelectedItems.Add(item);
                        }
                    }
                });
        }

        private void ExecuteIfNotUpdating(Action execute)
        {
            if (!this.updating)
            {
                try
                {
                    this.updating = true;
                    execute();
                }
                finally
                {
                    this.updating = false;
                }
            }
        }
    }
}

答案 2 :(得分:1)

答案 3 :(得分:1)

Drew Marsh的解决方案非常有效,我推荐它。我有另一个解决方案!

模型视图ViewModel是Passive View,您还可以使用Presentation Model访问演示文稿的某些数据,而无需与WPF结合使用 (此模式用于StocktraderPRISM示例。

答案 4 :(得分:1)

如果你有一个小清单,那么Drew Marsh的回答很好,如果你有一个很大的列表,那么找到所有你选中的项目的性能可能会很糟糕! 我最喜欢的解决方案是在ListBox上创建一个附加属性,然后绑定到包含所选项的ObservableCollection。 然后使用附加的属性,您订阅了SelectionChanged项目事件,以添加/删除您的集合中的项目。

答案 5 :(得分:1)

对我来说,最好的答案是打破MVVM的原则。

关于背后的代码            1. Instanciate你的viewModel            2.添加事件处理程序SelectionChanged            3.遍历所选项目并将每个项目添加到viewModel

列表中
ViewModel viewModel = new ViewModel();

viewModel.SelectedModules = new ObservableCollection<string>();

foreach (var selectedModule in listBox1.SelectedItems)
{
    viewModel.SelectedModules.Add(selectedModule.ToString());
}

答案 6 :(得分:0)

以下是View-Model-ViewModel Pattern的另一种变体,其中ViewModel可以通过IView接口访问视图。

我遇到了很多不能使用WPF绑定的场景,然后你需要一种代码来同步View和ViewModel之间的状态。

如何做到这一点如下所示:

WPF Application Framework (WAF)

答案 7 :(得分:0)

答案 8 :(得分:0)

David Rogers的解决方案很棒,详情请参阅以下相关问题:

Sync SelectedItems in a muliselect listbox with a collection in ViewModel