views:

61

answers:

1

Hey,

This is really peculiar. I tried a simple data binding example program. I tried to bind a collection (IList) to a list box. When i alter the collection, the list box is updated only if i maximize the window. Here are the snippets,

<ListBox x:Name="myBirthdaysListBox" ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <UniformGrid>
                <Label Content="{Binding Name}"></Label>
                <Label Content="{Binding DateOfBirth}"></Label>
            </UniformGrid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

public Window1()
{
    InitializeComponent();
    myCalendar = new List<Calendar>();
    myBirthdaysListBox.DataContext = myCalendar;
}

I am just a beginner in wpf. Kindly let me know, if i have done some thing terribly wrong in here.

A: 

Try using a BindingList<Calendar> rather than just a List<Calendar>, As binding list raises events for when items get added/removed/etc.

What you're seeing is the control is redrawing when you resize, and it's going through all the data again.

Off the top of my head, I believe you need to implement INotifyPropertyChanged on your Calendar such that the binding list is notified if an item in it changes

Rowland Shaw
Yep, this seems to work (i replaced the list with BindingList). If i would like to stick on to the list, should my Calendar class inherit the INotifyPropertyChanged interface ?? I tried doing so, it has no effect. Can you explain me why ?
sudarsanyes
What does your implementation of INotifyPropertyChanged look like?
Rowland Shaw
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Name")); PropertyChanged(this, new PropertyChangedEventArgs("DateOfBirth")); } I added this to the constructor of the Calendar class as its the only place from where you can set the values of the fields. So i am raising PropertyChanged for both the fields
sudarsanyes
I'd expect the event to be raised during change of the properties (after all, that's what the event represents). The BindingList<> will listen for this event, and raise the appropriate event for the control to listen to (this is why you'd use BindingList<> rather than List<>, as the controls are looking for an implementation of IBindingList, which List<> doesn't
Rowland Shaw