如何绑定 ObservableCollection在数据网格上?

发布于 2024-10-31 12:02:23 字数 571 浏览 1 评论 0原文

我是大卫。我不知道在 WPF 应用程序的 XAML 中将集合绑定到数据网格。

下面是课程。

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
{

}

上面的代码是Psedo代码。

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

I'm David. I don`t know to bind collection to datagrid in XAML for a WPF application.

below are classes.

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
{

}

above code is Psedo code.

DataContext has 7 items. So the grid will have 7 columns. Could someone please help me with an example or a code snippet.

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

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

发布评论

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

评论(3

明天过后 2024-11-07 12:02:23

如果数据上下文包含 TestCollection,则所需要做的就是将 ItemsSource 设置为 {Binding}

If the datacontext contains a TestCollection, all that is needed is Setting the ItemsSource to {Binding}

谁与争疯 2024-11-07 12:02:23

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

您的 viemodel :

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}”',它基本上意味着“我的列表框的源集合是我的数据上下文(它是一个Viewmodel对象)名为SourceList的集合”

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

I think that what you need is something like that :

Your viemodel :

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; }
}

You're view code behind

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

        DataContext = new ViewModel();

    }
}

And then in your view. The important stuff here is ' ItemsSource="{Binding SourceList}" ' which basically means "the source collection of my listbox is the the collection of my datacontext (which is a Viewmodel object) named SourceList"

        <ListView x:Name="listOne" 
              Grid.Column="0" 
              Width="50" 
              Height="200" 
              ItemsSource="{Binding SourceList}"  />
你是我的挚爱i 2024-11-07 12:02:23

我是新手,但我会冒险回答:

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

第二。语句执行绑定。

I am a newbie, but I will venture my answer:

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

The 2nd. statement performs the binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文