California - a very nice place to live.
Roaming around San Franciso is something that could be cherished. Watching and taking pictures on Pier 39, Downtown and the Golden Gate is so cool. Disney and Hollywood is a big WOW. One thing I like is that the people are so courteous and always smiling, greeting everybody around. Definitely a very nice place to live.
Let's discuss WPF's combo box binding. Once you learn it, it is very convenient to use.
There are a lot of sources and great blogs on how to use them.
Julie Lerma Blog and Microsoft's binding queen Beatriz Costa are one examples.
For here : I will just concentrate on binding ComboBox
For me, there are 2 important things to remember about comboboxes.
1. Where to retrieve data as combobox sources.
2. Where to save data of the SelectedValue or the Text displayed on the box.
ComboBox ItemSource
WPF Suggested to bind UIElement to an ObservableCollection to achieve high performance. I will not discuss binding to LINQ Entities here for LINQ entity results will have an enumerable / list. Let us create our own observable collection class.
WPF Suggested to bind UIElement to an ObservableCollection to achieve high performance. I will not discuss binding to LINQ Entities here for LINQ entity results will have an enumerable / list. Let us create our own observable collection class.
Assume that we have a Places.vb
-------------
Imports System.Collections.ObjectModel
Namespace PlaceDataSource
Public Class Places
Inherits ObservableCollection(Of String)
Public Sub New()
MyBase.Add("California")
MyBase.Add("New York")
End Sub
End Class
End Namespace
---------------
Now, inorder for you to use that class as your itemsource for the combobox, we need the following.
- Set the Namespace in the window namespace reference.
- Set the Resource of the window or page.
- Set combobox itemsource property to point to static resource declared by XAML.
1. Set the Namespace in the window namespace reference.
Declaration of resources can be declared in either window level or by application level. Let us use the window level to make it simple. Thus, on window xmls area we declare the namespace.
xmlns:places="clr-namespace:WpfApplication4.PlaceDataSource"
Note : That VS2008 designer will display all clr-namespaces as you type "xmlns:", then just select the PlaceDataSource Namespace.
2. Set the Resource of the window or page. - Right after referencing the namespace, we can set the resource of the window to.
<Window.Resources>
<places:Places x:key="PlaceBound" />
</Window.Resources>
3. Set combobox itemsource property to point to static resource declared by XAML. In setting the combobox itemsource, just drag your combobox in the window/page. then put the data source like below.
<ComboBox ItemsSource="{StaticResource PlacesBound}" />
and then... Run the program.. tsada... there you go... ComboBox will have its displaymember from our observablecollection....
As I've said, there are several ways to set combo's itemsource. You can bind it from XML, Enum, Enumerable.