如何绑定ObservableCollection 在Datagrid?

问题描述 投票:3回答:3

我是大卫。我不知道在XAML中为WPF应用程序将集合绑定到datagrid。

以下是课程。

class TestSetting(): INotifyChanged
{
   private a
   public double A
   {
      get a;
      set a = value;
      Notify("A");
   }

   private b
   public double B
   {
      get b;
      set b = value;
      Notify("B");
   }

   private c
   public double C
   {
      get c;
      set c = value;
      Notify("C");
   }

}


class TestCollect():ObservableCollection<T> ,INotifyListener
{

}

上面的代码是Pseudocode。

DataContext有7个项目。所以网格将有7列。有人可以帮我一个例子或代码片段。

wpf collections datagrid binding
3个回答
0
投票

如果datacontext包含TestCollection,那么所需的只是将ItemsSource设置为{Binding}


0
投票

我认为你需要的是这样的:

你的视图模型:

public class ViewModel
{
    public ViewModel()
    {
        SourceList = new ObservableCollection<BusinessAdapter>();

        for (int i = 0; i < 50; i++)
        {
            SourceList.Add(new BusinessAdapter { BusinessProperty = "blabla_" + i });
        }
    }
    public ObservableCollection<BusinessAdapter> SourceList { get; private set; }
}

你正在查看代码

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        DataContext = new ViewModel();

    }
}

然后在你看来。这里重要的东西是'ItemsSource =“{Binding SourceList}”',它基本上意味着“我的列表框的源集合是我的datacontext(它是一个Viewmodel对象)的集合,名为SourceList”

        <ListView x:Name="listOne" 
              Grid.Column="0" 
              Width="50" 
              Height="200" 
              ItemsSource="{Binding SourceList}"  />

0
投票

我是新手,但我会冒昧地回答:

ObservableCollection<YourModel> yourdata = new ObservableCollection<YourModel>();
dataGrid.ItemsSource = yourdata;

第二个。 statement执行绑定。

© www.soinside.com 2019 - 2024. All rights reserved.