如何将视图与多个 ObservableCollection 绑定

发布于 2024-07-21 07:55:42 字数 384 浏览 11 评论 0原文

我有一个带有多个 ObservableCollection 的 ModelView。 这是正确的方法吗?当视图调用视图模型时,所有 ObservableCollection 都需要用数据重新填充,然后再次对所有 CollectionViewSource 进行绑定。

另外,如何在视图模型的构造函数之外调用 CollectionViewSource.GetDefaultView,我收到一条错误,指出它只能在构造函数中调用。

如果我为每个 CollectionViewSource 创建一个单独的 ModelView,那么在将其中一个视图与 ModelView 绑定时,其余控件也会被绑定,但这次使用 null 值,并且所有 ModelView 都不会被调用。

我真的很困惑该怎么办,请帮忙。

I have a ModelView with multiple ObservableCollection. Is this the right approach and also when ever view calls the view modle, all the ObservableCollection needs to be re-populated with data and then binding takes place again for all the CollectionViewSource.

Also how do I call CollectionViewSource.GetDefaultView outside the constructor of the viewmodel, i get an error that it can only be called in the construtor.

If I create a seperate ModelView for each of the CollectionViewSource, then while binding one of the view with ModelView, rest of the controls also gets binded buth this time with null values and all all the ModelView is not called.

I am really confused what to do, please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-07-28 07:55:42

听起来你正在使用 MVVM。 您当然可以绑定到多个 ObservableCollections。 问题实际上是:你需要吗? 您应该保留与 ObserableCollections 的绑定,以防您的 ViewModel 发生更改并且需要使您的视图随更改而更新。

下面是我为您准备的一个示例,其中一个视图绑定到 ViewModel 中的两个 ObservableCollections 和一个 List。 所以——是的——你当然可以绑定到任何你想要的东西。 在这个例子中,两个 ObservableCollections 将交替更新。 这有帮助吗?

我在此处发布了此代码,如果它有助于拥有整个 vs 项目。

View:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Orientation="Vertical">

        <TextBlock>Bind to List:</TextBlock>
        <ListView ItemsSource="{Binding Path=Users}" Height="20"/>

        <TextBlock>Bind to ObservableCollection1:</TextBlock>
        <ListView ItemsSource="{Binding Path=ObservableCollection1}" 
                 Height="100"/>

        <TextBlock>Bind to ObservableCollection2:</TextBlock>
        <ListView ItemsSource="{Binding Path=ObservableCollection2}" 
                 Height="100"/>

    </StackPanel>
</Window>

ViewModel(View绑定到这个ViewModel)

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Timers;
using System.Windows;
using System.Windows.Threading;
namespace WpfApplication1
{
  public class Class1
  {
    public List<string> Users{get;set;}
    public ObservableCollection<string> ObservableCollection1 { get; set; }
    public ObservableCollection<string> ObservableCollection2 { get; set; }
    public Class1()
    {
      this.Users = new List<string>{ "bob", "mary" };

      this.ObservableCollection1 = new ObservableCollection<string>();
      this.ObservableCollection2 = new ObservableCollection<string>();
      int counter = 0;
      Timer t1 = new Timer();
      t1.Enabled = true;
      t1.Interval = 1000;
      t1.Elapsed += delegate
      {
        Application.Current.Dispatcher.Invoke(
        DispatcherPriority.Send, new Action(delegate
        {
          if(counter % 2 == 1)
            this.ObservableCollection1.Add(DateTime.Now.ToString());
          else
            this.ObservableCollection2.Add(DateTime.Now.ToString());
          ++counter;
        }));
      };
    }
  }
}

It sounds like you're using MVVM. You can certainly bind to multiple ObservableCollections. The question really is: do you need to? You should reserve binding to ObserableCollections for cases where your ViewModel is changing and you need to keep your View updated with the changes.

Here's an example I whipped up for you of a View bound to two ObservableCollections and one List in a ViewModel. So -- yes -- you can certainly bind to whatever you want. In this example, the two ObservableCollections will alternate updating. Does that help?

I posted the code for this here if it helps to have the whole vs project.

View:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Orientation="Vertical">

        <TextBlock>Bind to List:</TextBlock>
        <ListView ItemsSource="{Binding Path=Users}" Height="20"/>

        <TextBlock>Bind to ObservableCollection1:</TextBlock>
        <ListView ItemsSource="{Binding Path=ObservableCollection1}" 
                 Height="100"/>

        <TextBlock>Bind to ObservableCollection2:</TextBlock>
        <ListView ItemsSource="{Binding Path=ObservableCollection2}" 
                 Height="100"/>

    </StackPanel>
</Window>

ViewModel (View is bound to this ViewModel)

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Timers;
using System.Windows;
using System.Windows.Threading;
namespace WpfApplication1
{
  public class Class1
  {
    public List<string> Users{get;set;}
    public ObservableCollection<string> ObservableCollection1 { get; set; }
    public ObservableCollection<string> ObservableCollection2 { get; set; }
    public Class1()
    {
      this.Users = new List<string>{ "bob", "mary" };

      this.ObservableCollection1 = new ObservableCollection<string>();
      this.ObservableCollection2 = new ObservableCollection<string>();
      int counter = 0;
      Timer t1 = new Timer();
      t1.Enabled = true;
      t1.Interval = 1000;
      t1.Elapsed += delegate
      {
        Application.Current.Dispatcher.Invoke(
        DispatcherPriority.Send, new Action(delegate
        {
          if(counter % 2 == 1)
            this.ObservableCollection1.Add(DateTime.Now.ToString());
          else
            this.ObservableCollection2.Add(DateTime.Now.ToString());
          ++counter;
        }));
      };
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文