I want to get my ComboBox in XAML to bind to my List collection of custom objects in code behind.
Currently the ComboBox lists for each entry "dpwpf.Contact" which is my {namespace}.{classname}.
What do I need to put in the XAML to tell it to list out, e.g. LastName + FirstName?
I know it's something like {Binding Path=... Value=...} but I can't get it.
XAML:
<Window x:Class="dpwpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<StackPanel>
<TextBlock Text="Select the contact:"/>
<ComboBox Name="theContactList"/>
</StackPanel>
</StackPanel>
</Window>
Code Behind:
namespace dpwpf
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
StoreDB db = new StoreDB();
List<Contact> contacts = db.GetContacts()
theContactList.ItemsSource = contacts.ToList();
}
}
}
Answer:
<Window x:Class="dpwpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:local="clr-namespace:dpwpf">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Contact}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<StackPanel Margin="10">
<TextBlock Text="Contact Name:" Foreground="#555"/>
<TextBox Name="theName"/>
</StackPanel>
<StackPanel>
<TextBlock Text="Select the contact:"/>
<ComboBox Name="theContactList"/>
</StackPanel>
</StackPanel>
</Window>