31

I know this has been asked already but I have done almost everything what is suggested by developers.

<DataGrid x:Name="Imported" VerticalAlignment="Top"
          DataContext="{Binding Source=list}"
          AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Path=Date}"/>
    </DataGrid.Columns>
</DataGrid>

I am trying to show this in modal dialog box and populating the license list in the constructor of the modal dialog box. But still nothing is getting populated inside the DataGrid.

Constructor code:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
}
public class object
{
    string id;
    DateTime date;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }
}

Do you guys think something to do with the object list?

Toni
  • 1,555
  • 4
  • 15
  • 23
alice7
  • 3,834
  • 14
  • 64
  • 93
  • My list object has more other properties than the two above which im not binding? would that can make any diff? – alice7 Apr 27 '11 at 20:14
  • Probably not. Please share some more of your XAML (including how `list` is defined) so we can help further. – Dan J Apr 27 '11 at 20:17
  • I have added code on the top. – alice7 Apr 27 '11 at 20:44
  • @alice7 What type is the `diagboxclass`? What type is the XAML class `containing` the DataGrid? If `diagboxclass` is in the code-behind of the XAML file, you should assign `list` to the `DataContext` property of that class. You should then be able to bind to list from within the XAML. – Dan J Apr 27 '11 at 21:08
  • @djacobson: I got it resolved. Looks like xaml was tryin to load the list when the modal dialog was getting called and as the list was empty when the xaml was getting loaded. So what I did is not setting itemresources in xaml and tried on back hand inside constructor and that did the magic. – alice7 Apr 27 '11 at 21:24
  • I added this inside the constructor: Imported.ItemsSource = list; – alice7 Apr 27 '11 at 21:24
  • 2
    Hi, If one of the Answers helped you back in 2011 please mark it as Answer, thx – WiiMaxx Nov 04 '15 at 07:34

3 Answers3

45

PLEASE do not use object as a class name:

public class MyObject //better to choose an appropriate name
{
    string id;
    DateTime date;
    public string ID
    {
       get { return id; }
       set { id = value; }
    }
    public DateTime Date
    {
       get { return date; }
       set { date = value; }
    }
}

You should implement INotifyPropertyChanged for this class and of course call it on the Property setter. Otherwise changes are not reflected in your ui.

Your Viewmodel class/ dialogbox class should have a Property of your MyObject list. ObservableCollection<MyObject> is the way to go:

public ObservableCollection<MyObject> MyList
{
     get...
     set...
}

In your xaml you should set the Itemssource to your collection of MyObject. (the Datacontext have to be your dialogbox class!)

<DataGrid ItemsSource="{Binding Source=MyList}"  AutoGenerateColumns="False">
   <DataGrid.Columns>                
     <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
     <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>
Pozzi Userpic
  • 347
  • 8
  • 30
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • 7
    In my case the ItemSource of the DataGrid should be: ItemsSource="{Binding MyList}" – RHAD Nov 18 '17 at 08:54
24

Without seeing said object list, I believe you should be binding to the DataGrid's ItemsSource property, not its DataContext.

<DataGrid x:Name="Imported" VerticalAlignment="Top"
          ItemsSource="{Binding Source=list}"
          AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>

(This assumes that the element [UserControl, etc.] that contains the DataGrid has its DataContext bound to an object that contains the list collection. The DataGrid is derived from ItemsControl, which relies on its ItemsSource property to define the collection it binds its rows to. Hence, if list isn't a property of an object bound to your control's DataContext, you might need to set both DataContext={Binding list} and ItemsSource={Binding list} on the DataGrid).

Toni
  • 1,555
  • 4
  • 15
  • 23
Dan J
  • 16,319
  • 7
  • 50
  • 82
  • @alice7 Then please post some XAML showing the rest of your Window/UserControl/Whatever contains the grid, and let us know how `list` is defined. :) – Dan J Apr 27 '11 at 20:14
  • I have removed the entire content and just left the datagrid.so there is no other control. Here is the code isnide constructor: List objectList = new List(); objectList = GetObjectList(); – alice7 Apr 27 '11 at 20:18
  • 1
    Inside the constructor of... what? Please edit your question (click the `edit` link that appears beneath the tags) to include further code; it's pretty difficult to read in a comment. Thanks :) – Dan J Apr 27 '11 at 20:25
  • 2
    @alice7: You need to expose that list as a public property and set an appropriate DataContext, you should probably read [this reference](http://msdn.microsoft.com/en-us/library/ms752347.aspx). – H.B. Apr 27 '11 at 20:25
  • @djacobson: This one is yours. – H.B. Apr 27 '11 at 20:27
  • I have added all the code to the top that is there. And I know that list of objects is not null. – alice7 Apr 27 '11 at 20:40
  • 1
    The Binding in the ItemsSource Property is maybe wrong. In my case I was not successful when I declare the Binding with "{Binding Source=list}". But I was successful when I declare the Binding with "{Binding list}". – kbisang Mar 17 '16 at 16:52
5

Try to do this in the behind code:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
    Imported.ItemsSource = null;
    Imported.ItemsSource = list;
}

Also be sure your list is effectively populated and as mentioned by @Blindmeis, never use words that already are given a function in C#.

Toni
  • 1,555
  • 4
  • 15
  • 23
Schuere
  • 1,579
  • 19
  • 33