There is an CollectionChanged event in ObservableCollection collection for new elements added/removed.
Up vote 0 down vote favorite share g+ share fb share tw.
I have a ListBox, here I am binding IList Like (Cities). I want an event like OnItemDataBound in . NET in Windows Phone7.
Like when every city was binded (if 10 cities are this event will fire 10 times) this event will fire so that I have to do some more calculations on this event. In this event I have to find the object binded to the ListItem so that I can make some calculations. Is there any event in WP7 similar OnItemDataBound in .NET.
//Some data here I am binding the data like below: lstFlights. ItemsSource = objCities; on Binding every city I want an event to populate some list items(like: I want to change the textblock text,etc) according to the City that binded to ListItem. To do this I need an event like OnItemDataBound in WP7.
And I have list picker like below: On SelectionChanged event also I want to change the list items. And one more thing IList(objCities) is coming from the Service so I can't change that Object so if I want to change any TextBlock in List Box I have do FindName and I have to assign calculated value for every city binded. Windows-phone-7 link|improve this question edited Dec 21 '11 at 13:01ChristiaanV1,5821313 asked Dec 21 '11 at 11:22Mahi Kumar375 25% accept rate.
– ChristiaanV Dec 21 '11 at 11:29 the response I am getting from the service is very complex so I want do the calculations after binding it. And also I have a listpicker, on selection change also I have to change the calculations of ListBox, That's why I want OnItemDataBound event. Please tell if there is any event like that.
– Mahi Kumar Dec 21 '11 at 11:38 1 Can you show what you have right now? I'm not aware of an OnItemDataBound event, but for selection changes you could use the selectionchanged event to handle the situations where you have to do calculations on user selections. – ChristiaanV Dec 21 '11 at 11:46 Thanks for the reply ChristiaanV.
I have edited my question please see it once and tel me how can I proceed here. – Mahi Kumar Dec 21 '11 at 12:24.
There is an CollectionChanged event in ObservableCollection collection for new elements added/removed. ListBox provides some methods for accessing ListBoxItem. You can see them in list.
ItemContainerGenerator class. You can use it together to achieve needed results. There is short example that shows how to make a red foreground to newly added items in some conditions (even or not): ObservableCollection items = new ObservableCollection(); public MainPage() { InitializeComponent(); items.
CollectionChanged += (s, e) => { Dispatcher. BeginInvoke(() => { if (e. NewItems!
= null) { foreach (var item in e. NewItems) { ListBoxItem listitem = (ListBoxItem)list. ItemContainerGenerator.
ContainerFromItem(item); if (DateTime. Parse(listitem.Content.ToString()). Second % 2 == 0) { listitem.
Foreground = new SolidColorBrush(Color. FromArgb(255, 255, 0, 0)); } } } }); }; list. ItemsSource = items; } private void AddButton_Click(object sender, System.Windows.
RoutedEventArgs e) { items. Add(DateTime.Now. ToLongTimeString()); } private void RemoveButton_Click(object sender, System.Windows.
RoutedEventArgs e) { Random rnd = new Random(); if (items. Count > 0) { int index = rnd. Next(0, items.
Count); items. RemoveAt(index); } } }.
Thanks for the reply Ku6opr. But I don't have any ObservableCollection in My app. I have a IList and I am binding listbox using ItemsSource.
Here I want a event like OnItemDataBound . – Mahi Kumar Dec 21 '11 at 11:52.
This.myListBox. ItemContainerGenerator. ItemsChanged += new System.Windows.Controls.Primitives.
ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged); void ItemContainerGenerator_ItemsChanged(object sender, System.Windows.Controls.Primitives. ItemsChangedEventArgs e) { if (e. Action == NotifyCollectionChangedAction.
Add) ...
Thanks for the Reply Xin. But here how can I find my ListItems, for example I have a TextBlock in Listbox for that text box I have to bind price, So for this I will made some calculation and I will get price but how can I find the TextBlock in this event. In .net we will do like below in OnItemDataBound event: RepeaterItem item = (RepeaterItem)e.
Item; City city= (City)item. DataItem; Literal litPrice = (Literal)item. FindControl("litPrice"); – Mahi Kumar Dec 21 '11 at 13:04 in ItemsChanged event: DependencyObject listitem = list.
ItemContainerGenerator. ContainerFromIndex(e.Position. Index + e.Position.
Offset); DependencyObject child = VisualTreeHelper. GetChild(listitem, 0); and so on... – Ku6opr Dec 21 '11 at 13:20 thanks for the reply Ku6opr. But I am not able find listitem using DependencyObject listitem = list.
ItemContainerGenerator. ContainerFromIndex(e.Position. Index + e.Position.
Offset); listitem I am getting as null. – Mahi Kumar Dec 21 '11 at 14:25 Try this, var item = (ListBoxItem)myListBox. ItemContainerGenerator.
ContainerFromIndex(myListBox. Items?. Count - 1); – Xin Dec 21 '11 at 21:23 Xin:I have tried above code also but still listitem I am getting as null.
– Mahi Kumar Dec 21 '117 at 6:07.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.