'How to bind ObservableCollection<T> on Datagrid?

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.



Solution 1:[1]

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

Solution 2:[2]

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}"  />

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emond
Solution 2 Bruno
Solution 3 Travis Banger