Trying to learn how to bind objects to various types of controls. In this instance, I want to get sample data in my object to appear in ComboBox. The code runs but what appears instead of values (David, Helen, Joe) is text "TheProtect.UserControls.Client")
XAML: (ucDataBindingObject.xaml)
<UserControl x:Class="TheProject.UserControls.ucDataBindingObject" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Width="Auto" Height="Auto" mc:Ignorable="d"> <Grid Width="130" Height="240" Margin="0"> <ComboBox Width="310" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding Path=Clients}" /> </Grid> </UserControl>
C#: ucDataBindingObject.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Controls; namespace TheProject.UserControls { public partial class ucDataBindingObject : UserControl { public List<Client> Clients { get; set; } public ucDataBindingObject() { Clients = new List<Client>(); Clients.Add(new Client(1, "David")); // sample data Clients.Add(new Client(2, "Helen")); Clients.Add(new Client(3, "Joe")); InitializeComponent(); this.DataContext = this; } }
C# Client.cs
using System; using System.Linq; namespace TheProject.UserControls { public class Client { public int ID { get; set; } public string Name { get; set; } public Client(int id, string name) { this.ID = id; this.Name = name; } } }
This article shows how to bind data dynamically from the database and get the ComboBox selected Text and Value. In ComboBox Element set the attribute ItemSource="{Binding}". Here DisplayMemberPath helps to display Text in the ComboBox. SelectedValuePath helps to store values like a hidden field.
The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.
DisplayMemberPath specifies the path to the display string property for each item. In your case, you'd set it to "Name" , not "{Binding Name}" .
When Cb_OnPreviewTextInput is called I set IsDropdownOpen = true . In the first attempt (after typing the first letter) the first item on the list is selected and I can go up and down using relevant arrows, the caret is still in the TextBox .
There are several ways to tell the framework what to display
1) Use DisplayMemberPath
on the ComboBox (this will display the named property):
<ComboBox ItemsSource="{Binding Path=Clients}" DisplayMemberPath="Name" />
2) Set ItemTemplate
on the ComboBox. This is like #1, except allows you to define a template to display, rather than just a property:
<ComboBox ItemsSource="{Binding Path=Clients}"> <ComboBox.ItemTemplate> <DataTemplate> <Border BorderBrush="Green" BorderThickness="1" Padding="5"> <TextBlock Text="{Binding Path=Name,StringFormat='Name: {0}'}" /> </Border> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
3) Add a ToString()
override to source class. Useful if you always want to display the same string for a given class. (Note that the default ToString()
is just the class type name, which is why you see "TheProtect.UserControls.Client".)
public class Client { // ... public override string ToString() { return string.Format("{0} ({1})", Name, ID); } }
4) Add a DataTemplate
to the XAML resources. This is useful for associating a given class type with a more complex or stylized template.
<UserControl xmlns:local="clr-namespace:TheProject.UserControls"> <UserControl.Resources> <DataTemplate DataType="local:Client"> <TextBlock Text="{Binding Name}" /> </DataTemplate> </UserControl.Resources> // ... </UserControl>
In DisplayMemberPath
, give the name of the property which you want to show in the comboBox. In SelectedValuePath
, give the name of the property which you want to select. When you do a ComboBox.SelectedValue
, you will get the value of this property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With