Answered by:
Listview data binding question

Question
-
Hi,
I have created a listview and I have bound it to ObservableCollection(Of UserInfo) list. The listview has 2 columns and I have bound the columns to properties in my object. When I add items to the collection I see them in my listview but the SourceUpdate event never gets fired. Is there anything specially needs to be done to get the event fired. I need to do more stuff once the user added to the list. Some code snippet below
XAML:
<ListView ItemsSource="{Binding Source={StaticResource UsersListDataSource}, NotifyOnSourceUpdated=true}" IsSynchronizedWithCurrentItem="True" Margin="4,9,5,9" Name="lvUserList" SelectionMode="Single" SelectionChanged="OnListViewSelectionChanged" SourceUpdated="OnSourceUpdated" TargetUpdated="OnTargetUpdated">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=UserName, Mode=OneWay}" Width="158" Header="User Name"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=UserType, Mode=OneWay}" Width="158" Header="Autority Level"/>
</GridView>
</ListView.View>
</ListView>Monday, November 23, 2009 7:52 PM
Answers
-
You should be right to do something like this...public partial class Window1 : Window{public Window1(){InitializeComponent();this.Loaded += new RoutedEventHandler(Window1_Loaded);}void Window1_Loaded(object sender, RoutedEventArgs e){ObservableCollection<string> myStrings = new ObservableCollection<string>();myStrings.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(myStrings_CollectionChanged);myStrings.Add("Item 1");myStrings.Add("Item 2");myStrings.Remove("Item 2");}void myStrings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e){// This gets called when items are added or deleted from the ObservableCollectionif (e.NewItems != null){foreach (string s in e.NewItems){Console.WriteLine(string.Format("Item added : {0}", s));}}if (e.OldItems != null){foreach (string s in e.OldItems){Console.WriteLine(string.Format("Item removed : {0}", s));}}}}Warm regards,MattP.S Sorry for the late response!
- Proposed as answer by Geert van Horrik Monday, November 30, 2009 4:45 PM
- Marked as answer by Jay_WangMicrosoft employee Tuesday, December 8, 2009 2:42 AM
Friday, November 27, 2009 12:03 AM
All replies
-
Try hooking into the ObservableCollections CollectionChanged event.Regards,MattMonday, November 23, 2009 11:31 PM
-
Thanks Matt, It is already using ObservableCollection. I have been trying all I know to get an event once the collection changes whether an addition user has been added or deleted or modified but I don't seem to be doing it right because I dont get SourceUpdated event fired off. Below is the collection code. I can post more of my code if it helps more.
Public Class UsersList
Inherits ObservableCollection(Of UserInfo)
Private bolIsModified As Boolean
Public Sub New()
Dim strarrUserName() As String = Nothing
Dim strarrPassword() As String = Nothing
Dim strarrUserType() As String = Nothing
Dim bolarrReserved() As Boolean = Nothing
Try
GetUserList(strarrUserName, strarrPassword, strarrUserType, bolarrReserved)
For intCounter As Integer = 0 To strarrUserName.Length - 1
MyBase.Add(New UserInfo(strarrUserName(intCounter), strarrPassword(intCounter), strarrUserType(intCounter), bolarrReserved(intCounter)))
Next intCounter
Catch ex As Exception
End Try
End SubTuesday, November 24, 2009 3:31 PM -
You should be right to do something like this...public partial class Window1 : Window{public Window1(){InitializeComponent();this.Loaded += new RoutedEventHandler(Window1_Loaded);}void Window1_Loaded(object sender, RoutedEventArgs e){ObservableCollection<string> myStrings = new ObservableCollection<string>();myStrings.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(myStrings_CollectionChanged);myStrings.Add("Item 1");myStrings.Add("Item 2");myStrings.Remove("Item 2");}void myStrings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e){// This gets called when items are added or deleted from the ObservableCollectionif (e.NewItems != null){foreach (string s in e.NewItems){Console.WriteLine(string.Format("Item added : {0}", s));}}if (e.OldItems != null){foreach (string s in e.OldItems){Console.WriteLine(string.Format("Item removed : {0}", s));}}}}Warm regards,MattP.S Sorry for the late response!
- Proposed as answer by Geert van Horrik Monday, November 30, 2009 4:45 PM
- Marked as answer by Jay_WangMicrosoft employee Tuesday, December 8, 2009 2:42 AM
Friday, November 27, 2009 12:03 AM -
Hi,
since you inherit from PbservableCollection, you can override InsertItem and RemoveItem so that you're notfied properly when items are added or removed. Also, the constructor should probably call MyBase.New. And, FWIW, Beth Massi has demonstrated a how-to-video on utilizing ObservableCollections resp. inheriting from it using VB.net. I don't recall exactly what video it was, but it was either the WPF over data or the WPF over Entity Framework series.
Cheers,
OlafFriday, November 27, 2009 7:05 AM -
What are you actually trying to achieve and why would you want to subscribe to the Changed event at all? Why not just looping the UsersListDataSource at the end of the window or control lifecycle?
Also, your binding can be somewhat simpler:
Code-behind:ObservableCollection<UserInfo> userCollection = new ObservableCollection<UserInfo>(); // Fill your collection here, or somewhere else Resources["MyUserCollection"] = userCollection;
Xaml:<ListView ItemsSource="{DynamicResource MyUserCollection}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=UserName, Mode=OneWay}" Width="158" Header="User Name"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=UserType, Mode=OneWay}" Width="158" Header="Autority Level"/>
</GridView>
</ListView.View> </Listview>
Saves you some typing.
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
Looking for a way to deploy your updates to all your clients? Try Updater!Monday, November 30, 2009 4:50 PM