# .NET and More > WPF, WCF, WF >  ListView ItemsSource DataTable

## Sybre

I have a listview in details mode which I am using to display the contents of a datatable.  At the moment I am setting the ListView ItemsSource in the vb code after populating the table like this:


VB Code:
Class Window1
    Dim oTable As New DataTable
     Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        GetData()   'Sub to populate datatable
        ListView1.ItemsSource = oTable.DefaultView   'assign the datatable to the listview itemssource
    End Sub

This works fine but I was wondering if anyone can tell me how I can assign the listview ItemsSource to the datatable in the XAML?

----------


## chris128

Why would you want to do it in XAML if you are only loading the data at runtime? As far as I know, if you set it in XAML then you will just end up with an empty listview even after you have loaded the DataTable because DataTable's dont support the WPF change notification system. You could get around this by adding each item from your DataTable into an ObservableCollection (basically a List(Of T) that automatically does change notification for you) but to be honest I prefer doing it via code anyway because then you dont have to mess around with DataContexts. I spent ages when I first started using WPF just trying to do what you are wanting to do (but with an ObservableCollection) and in the end just decided that it was a lot easier to just set ItemsSource in my Window's Loaded event.

----------


## Sybre

This is just my simple test app for figuring out how stuff works - I'm new to WPF and am mostly looking into what is and isn't possible.
It's my intention to refactor a windows forms app into WPF but that app is much more complicated with multiple datatables and a lot of databound controls so I thought binding in the xaml would make things much clearer.
If this can't be done then I'll just have to stick with doing it in the vb, it just seemed like it should be possible.

----------


## chris128

Well it is possible but as I said it might not update the listbox once you actually get some data into your DataTable so if you want your listbox to update automatically you might want to look at the ObservableCollection. Although I assume there must be some way to make it automatically update with a DataTable as you cant use an ObservableCollection for everything..

----------


## Sybre

I don't think using datatables is a problem - I've just stuck another sub in which adds more data to the datatable and gets called on a button click (ie after the datatable has been set as the ItemsSource) and it updates the listbox fine.

----------


## chris128

Oh right cool maybe they just work then  :Smilie: 
Well, if you want to just try setting the ItemsSource in XAML you should just be able to create a public property for your DataTable and then set something like:

<Listview ItemsSource="{Binding MyDT}" />

Of course that is assuming you named the property MyDT
You might also need to mess around with the DataContext before that will work (something I'm not too familiar with to be honest) but give it a go

----------


## Sybre

Nope, neither ItemsSource="{Binding oTable}" or ItemsSource="{Binding oTable.DefaultView}" work - they don't give any errors but nothing appears in the listview.
Don't know anything about the DataContext so not sure where to start messing around with that...

----------


## chris128

Yeah but is oTable a public property?

----------


## Sybre

Um, don't know - how can I tell/set it?

----------


## chris128

Declaring a property is a pretty basic thing, the common way to do this is to create a private variable and then create a public property that just 'publishes' this private variable, something like this:


vb.net Code:
Private _oTable As New DataTable
 Public Property oTable As DataTable
   Get
       Return _oTable
   End Get
   Set(ByVal value As DataTable)
       _oTable = value
   End Set
End Property

----------


## Sybre

Oh, sorry, see what you meant now (was trying to figure out what it meant in XAML - I blame it on it being friday!).

Anyway, still does nothing  :Frown:

----------


## NStuff42

Why would you want to set the itemsource in XAML when you are populating the data in code?

----------

