0
votes

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.

<ListBox Loaded="lstFlights_Loaded" Height="635" x:Name="lstFlights"   
                         ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border CornerRadius="10" Margin="10">
                                <Border.Background>
                                    <ImageBrush ImageSource="../Images/rowbg.png"></ImageBrush>
                                </Border.Background>
//Some data here
     </Border>

                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

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.

2
Why can't you do the calculations before binding it to the list?ChristiaanV
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
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
Thanks for the reply ChristiaanV. I have edited my question please see it once and tel me how can i proceed here.Mahi Kumar

2 Answers

1
votes

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<string> items = new ObservableCollection<string>();

    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);
        }
    }
}
1
votes

How about using the ItemsChanged event of your ListBox's ItemContainerGenerator instead?

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) 
    ...