Can DataBinding Do this?

I have a dataset that gets loaded with a list of contacts including first, last, and email address. Instead of listing all of the contacts in the dataset i would like to search the email_address column for a specific email address and display the first name and last name in a textbox. Is this possible to do so?

Thanks!

[381 byte] By [A.Carter] at [2008-1-8]
# 1

Hi,

I don't think you can do it simply with a DataSet, but you can do it with a Collection. With a Collection you would create a new ICollectionView, with its Filter property set to a delegate that would only keep the entries you're interested in. Then in the control's ItemTemplate you could easily display the first and last names of the entries that passed the filter.

Luc

LucVoVan at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

Thanks Luc for the reply, I found a couple of examples to implement the ICollectionView, However i'm not too sure how to set it up using ItemsTemplate. Could you possibly point me in the right direction?

Thanks

A.Carter at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

Hi again,

I just read your first post again, and I read it too quickly at first, you don't seem to want to display many items but just one. Using ICollectionView is good for ItemControls (like ListView/ListBox) but for just one item, it's easier to manually find the instance you are looking for (by hand), assign it to a class property, and bind to that property.

For example:

Set MyWindow.SelectedPerson to the instance you are looking for, and bind to it in your XAML file <TextBox Text={Binding Path=SelectedPerson.FirstName}>.

For the ItemsControl/ICollectionView way, which would allow multiple textboxes if the filter returns more than one match (and if you are still interested in that solution, of course )

You don't set an ICollectionView via ItemTemplate: you set ICollectionView.Filter procedurally to filter the items that are going to be displayed via ItemTemplate. But to help you debug/understand the way it works, consider writing your ItemTemplate before trying out ICollectionView features. Once you are sure that your ItemTemplate works for your whole Collection, try filtering the Collection.

Luc

PS: I hope I'm not getting too far from the original subject!
LucVoVan at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

Thanks alot Luc. I didn't think about setting a property with the information. It would definatly be easier to do it this way. Instead of a property of the class i was thinking about creating a TextBoxClass inheriting from textbox. Then i would override the Tag property and use that to set the email address and then create an event to fire everytime the tag property is changed (i didn't see an event for like a "TagChanged"). If it did change, then the eventhandler would search that data table for the contact and apply it. What are your thoughts on this?

Thanks again!

A.Carter at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

I think that it's very Windows Form-ish way of doing things (inheriting, Tag, normal event), but it would work. WPF works more with bindings which leads to things updating their display automatically.

The suggestion I gave you earlier, the property, would be the way I'd do it. The only problem with this approach is updating the value of the property, because in this case the binding is only re-evaluated when your class triggers a IPropertyChanged.PropertyChanged event...which you'll have to call at some point. Maybe everytime the DataSet is changed, or depending on your application, at some more "optimized" point in time.

Luc

LucVoVan at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified