|
-
Aug 6th, 2009, 11:14 AM
#1
Thread Starter
Member
ListView ItemsSource DataTable
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?
-
Aug 6th, 2009, 01:23 PM
#2
Re: ListView ItemsSource DataTable
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.
-
Aug 7th, 2009, 04:30 AM
#3
Thread Starter
Member
Re: ListView ItemsSource DataTable
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.
-
Aug 7th, 2009, 04:37 AM
#4
Re: ListView ItemsSource DataTable
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..
-
Aug 7th, 2009, 04:41 AM
#5
Thread Starter
Member
Re: ListView ItemsSource DataTable
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.
-
Aug 7th, 2009, 04:47 AM
#6
Re: ListView ItemsSource DataTable
Oh right cool maybe they just work then 
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
-
Aug 7th, 2009, 05:00 AM
#7
Thread Starter
Member
Re: ListView ItemsSource DataTable
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...
-
Aug 7th, 2009, 05:19 AM
#8
Re: ListView ItemsSource DataTable
Yeah but is oTable a public property?
-
Aug 7th, 2009, 05:38 AM
#9
Thread Starter
Member
Re: ListView ItemsSource DataTable
Um, don't know - how can I tell/set it?
-
Aug 7th, 2009, 05:46 AM
#10
Re: ListView ItemsSource DataTable
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
-
Aug 7th, 2009, 05:51 AM
#11
Thread Starter
Member
Re: ListView ItemsSource DataTable
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
-
Aug 8th, 2009, 11:02 AM
#12
Addicted Member
Re: ListView ItemsSource DataTable
Why would you want to set the itemsource in XAML when you are populating the data in code?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|