Modifying a DataBound Collection - Page 6
       by kirupa  |  9 December 2007

In the previous page, we started to separate some of the UI specific tasks from the data-specific tasks...which essentially amounted to copying pasting existing code into a new class file. In this page, we'll do something a bit more ambitious!

Taking Care of PeopleData
Let's now take care of our PeopleData class, for it will play a crucial role in re-enabling your button to add items to our list. Yes, I am aware that this means you will temporarily break a fully functioning application.

 This time around, we can't just copy and paste some code and expect everything to work. There is some major reshuffling that will need to take place. First, copy and paste the following lines of code into your PeopleData class (which is currently empty):

class PeopleData
{
public static ObjectDataProvider PeopleDataSource
{
get;
set;
}
 
public static void AddPerson(string name)
{
Person newPerson = new Person();
newPerson.PersonName = name;
 
(PeopleData.PeopleDataSource.Data as PeopleList).Add(newPerson);
}
}

Notice what I am doing here. I first declare a static property called PeopleDataSource that is of type ObjectDataProvider.

Next, I declare a static method called AddPerson, and this method takes a string object called name as its argument. Within this method, I am re-creating the functionality our AddButton_Click event handler had originally:

public static void AddPerson(string name)
{
Person newPerson = new Person();
newPerson.PersonName = name;
 
(PeopleData.PeopleDataSource.Data as PeopleList).Add(newPerson);
}

I declare a new Person object, and I then set its PersonName property to the name argument I pass in. Next, I reference our PeopleDataSource property and add the newPerson Person object I just created. In case you are unfamiliar with the text preceding our Add method, when dealing with static methods, you can’t just use the this keyword and reference the property defined inside this class.

All of this is great, but currently, you actually aren’t using these newly created methods at all. That is something that needs to be fixed, but before we dive into that, let’s first look at our game plan.

Initially, our PeopleDataSource property is going to be null. It won’t be storing any values at all unless it is first initialized to the object data provider / data source that already exists. In other words, we can’t call our AddPerson method until our PeopleDataSource method points to data source used by the data binding.

What we are going to do is, shortly after our Window is initialized, assign our PeopleListDS data provider to the PeopleDataSource property. How would we know when our window has been initialized? Well, fortunately, there is actually an event that gets fired once a Window is initialized, and this event fires before most other events that you may have running in your application. Intercepting our window's initialized event and initializing our PeopleDataSource property ensures that our PeopleDataSource property has a value assigned to it very early in our application’s life.

To actually do implement what I wrote in the previous paragraph, you'll have to go back to Blend – there is a reason why I didn’t ask you to close it earlier! Switch back into Blend and select your Window from Objects and Timeline:

[ select your Window parent from Objects and Timeline ]

Once you have selected your Window, glance over at your Property pane/grid and click on the Events button to display a list of all events your selected Window allows you to modify. Scroll down this list to find the Initialized event, and in the textbox next to it where you specify the name of the event handler, enter the name WindowInitialized:

[ give your Initialized event an event handler named WindowInitialized ]

Press Enter once you have given your Initialized event the event handler name WindowInitialized. Once you press Enter, Visual Studio will steal focus from Blend and display the WindowInitialized event handler it created for you:

private void WindowInitialized(object sender, EventArgs e)
{
 
}

To reiterate what I mentioned earlier, when your application has loaded, your Initialized event is fired immediately. In our case, the Initialized event is intercepted by the WindowInitialized event handler, and it is here where we want to take care of initializing our PeopleDataSource property. Add the following line of code into your WindowInitialized method:

PeopleData.PeopleDataSource = this.FindResource("PeopleListDS") as ObjectDataProvider;

This line is almost the same as what you had earlier. The only difference is that you are assigning the data provider already in existence to our PeopleData’s PeopleDataSource static property. Your entire WindowInitialized method with the above code copied and pasted into it should look like the following:

private void WindowInitialized(object sender, EventArgs e)
{
PeopleData.PeopleDataSource = this.FindResource("PeopleListDS") as ObjectDataProvider;
}

All right! We just finished setting up our PeopleData class, and we also hooked up our Initialized event to the WindowInitialized event handler. Right now, what you just did may not make much sense, but I will explain all of this in one fell swoop in the next page after we modify your AddButton_Click event handler to use the PeopleData class we modified in this page.

Onwards to the next page!

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.