【问题标题】:Region or ItemsSource for large data set in ListBoxListBox 中大型数据集的 Region 或 ItemsSource
【发布时间】:2026-01-13 07:20:06
【问题描述】:

在以下情况下,我无法确定最佳解决方案。我正在使用 Prism 4.1、MEF 和 .Net 4.0。

我有一个对象Project,它可能有大量(~1000)个Line 对象。我正在决定是否最好从我的ProjectViewModel 公开ObservableCollection<LineViewModel> 并在那里手动创建 Line 视图模型,或者将 ListBox 设置为它自己的区域并以这种方式激活视图。

我仍然希望我的LineViewModel 能够注入 Prism 的共享服务(IEventAggregator 等),但是当我手动创建 LineViewModel 时我不知道该怎么做。有什么建议或想法吗?

编辑:我最初的想法:

项目:

public class Project
{
    public List<Line> Lines { get; set; }
}

ProjectViewModel:

[Export(typeof(ProjectViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ProjectViewModel : NotificationObject, IRegionMemberLifetime
{
    private Project _localProject;

    /* 
        HERE WILL BE SOME PROPERTIES LIKE COST, PRICE THAT ARE CUMULATIVE FROM THE Lines 
     */

    public ObservableCollection<LineViewModel> Lines { get; private set; }

    private readonly IEventAggregator _eventAggregator;

    [ImportingConstructor]
    public ProjectViewModel(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        _eventAggregator.GetEvent<ProjectLoaded>().Subscribe(SetupProject, false);
        Lines = new ObservableCollection<LineViewModel>();
    }

    private void SetupProject(Project project)
    {
        _localProject = project;

        foreach(var l in _localProject.Lines)
        {
            LineViewModel lvm = new LineViewModel(l);
            lvm.PropertyChanged += // Some handler here
            Lines.Add(lvm);
        }
    }

    public bool KeepAlive
    {
        get { return false; }
    }
}

LineViewModel:

public class LineViewModel : NotificationObject
{
    private Line _localLine;

    public decimal Cost
    {
        get { return _localLine.Cost; }
        set
        {
            _localLine.Cost = value;
            RaisePropertyChanged(() => Cost);
        }
    }

    public LineViewModel(Line incoming)
    {
        _localLine = incoming;
    }
}

【问题讨论】:

  • 啊,我明白了。它看起来不错,也许你应该继续前进,看看它是如何运作的。至于使用 DI,只需将 LineViewModel 更改为具有 Line 属性,而不是在构造函数中传递它。使用容器解析 LineViewModel(因此获取注入的依赖项),然后设置 LineViewModel.Line = l;你应该很高兴。

标签: c# wpf prism mef prism-4


【解决方案1】:

我在这里可能有点离题,也许这太简单了,但这对你有帮助吗?我创建了一个快速项目,演示了一些基础知识。如果您需要更多信息,也许我可以为您提供更多帮助。

Sample Application With Binding To "Lines"

查看

    <Window x:Class="WpfApplication1.LinesView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LinesView" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="247" d:DesignWidth="348" SizeToContent="WidthAndHeight" Width="350" Height="250">
    <Window.Resources>
        <DataTemplate x:Key="LineView">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto" MinWidth="50"/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0" Text="Line: " />
                <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name}" />

                <TextBlock Grid.Row="1" Grid.Column="0" Text="X: " />
                <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding X}" />

                <TextBlock Grid.Row="2" Grid.Column="0" Text="Y: " />
                <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Y}" />
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="Total Cost" Margin="5" />
            <TextBlock Text="{Binding Cost}" Margin="5" />
        </StackPanel>
        <ContentControl Name="contentControl1" Content="{Binding ElementName=listBox1, Path=SelectedItem}" ContentTemplate="{StaticResource LineView}" VerticalAlignment="Center" HorizontalAlignment="Center" Width="105" Margin="5" />
        <ListBox Height="234" 
                 HorizontalAlignment="Center"
                 Name="listBox1" 
                 VerticalAlignment="Center"
                 ItemsSource="{Binding Lines}"
                 ItemTemplate="{StaticResource LineView}" Width="152" Margin="5" />
    </StackPanel>
</Window>

视图模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfApplication1.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication1
{
    public class LinesViewModel : INotifyPropertyChanged
    {
        public int Cost
        {
            get
            {
                return Lines.Sum(x => x.X + x.Y); 
            }
        }

        public ObservableCollection<Line> Lines
        {
            get;
            private set;
        }

        public LinesViewModel()
        {
            Lines = new ObservableCollection<Line>();
            Lines.Add(new Line()
            {
                Name = "Line1",
                X = 0,
                Y = 1
            });
            Lines.Add(new Line()
            {
                Name = "Line2",
                X = 1,
                Y = 1
            });
            Lines.Add(new Line()
            {
                Name = "Line3",
                X = 2,
                Y = 2
            });

            foreach(Line line in Lines)
            {
                line.XChanged += new EventHandler(lineChanged);
                line.YChanged += new EventHandler(lineChanged);
            }

            Lines.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Lines_CollectionChanged);
        }

        private void Lines_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Line line in e.NewItems)
                {
                    line.XChanged += new EventHandler(lineChanged);
                    line.YChanged += new EventHandler(lineChanged);
                }
            }
            if (e.OldItems != null)
            {
                foreach (Line line in e.OldItems)
                {
                    line.XChanged -= new EventHandler(lineChanged);
                    line.YChanged -= new EventHandler(lineChanged);
                }
            }
        }

        private void lineChanged(object sender, EventArgs e)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("Cost"));
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    }
}

型号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication1.Models
{
    public class Line
    {
        private int x;
        private int y;

        public String Name { get; set; }

        public int X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
                XChanged(this, EventArgs.Empty);
            }
        }

        public int Y
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
                YChanged(this, EventArgs.Empty);
            }
        }

        public event EventHandler XChanged = delegate { };
        public event EventHandler YChanged = delegate { };
    }
}

【讨论】:

  • 这与我最初的设计非常接近。不同之处在于 1) 我的 Line 对象上没有事件,2) 我的 ProjectViewModel 将是 Lines ObservableCollection 的所有者。我仍然认为拥有一个 LineViewModel 集合对我有好处,每个 LineViewModel 都包含一个 Line 对象……它将充当 Line 对象的代理,因为 Line 没有任何事件或实现 INotifyPropertyChanged。
  • 考虑一个订单输入系统。您有一个订单,然后是订单项。订单有一些属性,其计算基于行项目。订单项对象非常基本(只是一堆自动属性;成本、价格、数量)。我认为一个新对象(LineViewModel)将保留一个订单项对象并控制设置订单项的属性。我还认为“订单”(我的 ProjectViewModel)对象应该拥有所有这些 LineViewModel。
  • 另外,非常感谢您花时间编写示例应用程序。虽然这不是我想要的,但很高兴你能抽出时间提供帮助。
  • @Ryan 如果您的 Lines 没有实现 INotifyPropertyChanged 并且没有事件,那么当您将它们包装在 ViewModel 中时,您如何检测它们的变化?看来您必须这样做才能更新您的费用或其他任何内容。
  • 我添加了代码来进一步解释我是如何考虑设置它的。
【解决方案2】:

要使用 Prism/MEF 手动创建 LineViewModel,您可以使用容器来解决依赖关系,这就是它的用途。

例如,

LineViewModel line = container.GetExportedValue<LineViewModel>();

查看此链接:Managing Dependencies: Resolving Instances With MEF

我有点担心您的设计,您的每一行是否真的有必要拥有一个 ViewModel 并由容器创建并注入依赖项?是否有可能有一个对象管理所有行并具有那些注入的依赖项?也许某种存储库模式可能会让您受益?

如果您通过容器解析数千个对象,则可能会产生相当大的开销。棱镜书还提到这可能不是一个好主意Considerations for using the container

【讨论】:

  • 感谢您的回复。如果你不介意,我对我的设计有点难过,所以我不确定什么我想做什么。正如我上面提到的,我有一个带有 ProjectView/ViewModel 的 Project 对象。这个项目有线条,我想在列表框中以某种方式显示。这些行需要能够通知 ProjectViewModel 属性更改,以便 Project 模型可以重新计算成本。项目对象来自反序列化的 XML,并且已经包含它的行。
  • 我会有类似的东西。我有需要从 XML 文件中读取到列表中的对象。该“列表”作为单个对象绘制在图形上,并且该列表也被添加到 ListBox 以创建图形图例,允许我配置表示列表的图形对象的可见性和颜色。因此,在我的例子中,对象是从 XML 中读取的,可能存储在 ObservableCollection 中,而 ViewModel 包含该集合并公开了一个 Name、Color 和 Visible 属性等。但是,在某些方面我可能和你有同样的问题。
  • 我还没有开始,但是这些对象的 oif 属性可能会发生变化,图表可能需要更新以反映这种变化。对我来说,我将有一个代表整个列表的图形对象,我猜想该对象将需要处理 CollectionChanged 事件并将事件处理程序连接/取消连接到应该实现 INotifyPropertyChanged 的​​各个对象。然后,作为一个整体的对象可以重新获得它需要的东西,并更新自己。这可能是几千个事件处理程序,但它可能没问题,因为只有少数会同时发生变化。
  • 无论如何,在我看来,您的线条实际上可能更多是您的模型而不是视图模型。我没有理由让容器解析我的对象,它们只是从文件中读取出来的。
  • 正确,我的线条是我模型的一部分...它们在项目对象上。但是因为我要通过 UI 与行上的属性进行交互,所以我打算创建一个 LineViewModel 对象集合并将其绑定到 ListBox。我只是不确定这是否是正确的方法。我的模型对象没有实现 INotifyPropertyChanged,所以无论如何我都需要将它们包装在视图模型中。