Hi,
I tried to make up an example to show my problem.
My combobox has a list of objects as itemssource. In my example it's a list of Persons. In the combobox i want to show the first name and the last name of the person. But i want to save the last name of the person in the "owner" property of the house-object.
My guess was that i bind the SelectedValue to my property and the SelectedValuePath to the name of the property in the comboboxitem.
I already googled and tried a view other versions but nothing worked.
If i use SelectedItem instead of SelectedValue with the same binding at least the value of the "tostring" function get's written in the property. Sadly that solution doesn't fit in the Rest of my Program because i don't want to override "ToString".
The Xaml:
<Window x:Class="MultiColumnCombobox.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"
x:Name="window">
<Grid>
<ComboBox Height="23"
Margin="72,12,86,0"
Name="ComboBox1"
VerticalAlignment="Top"
SelectedValue="{Binding CurrentHouse.Owner, ElementName=window, Mode=TwoWay}"
SelectedValuePath="LastName"
ItemsSource="{Binding PersonList, ElementName=window, Mode=Default}">
<ComboBox.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}"
Padding="10,0,0,0" />
<TextBlock Text="{Binding Path=LastName}"
Padding="10,0,0,0" />
</WrapPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Height="23"
Click="PrintButton_Click"
HorizontalAlignment="Left"
Margin="12,0,0,9"
Name="PrintButton"
VerticalAlignment="Bottom"
Width="75">Print</Button>
</Grid>
The C#
using System.Collections.Generic;
using System.Windows;
using System;
namespace MultiColumnCombobox
{
public partial class Window1 : Window
{
private List _PersonList = new List();
public List PersonList
{
get { return _PersonList; }
set { _PersonList = value; }
}
private House _CurrentHouse = new House { Owner = "Green", Number = "11" };
public House CurrentHouse
{
get { return _CurrentHouse; }
}
public Window1()
{
InitializeComponent();
PersonList.Add(new Person {FirstName = "Peter", LastName = "Smith"});
PersonList.Add(new Person {FirstName = "John", LastName = "Meyer"});
PersonList.Add(new Person {FirstName = "Fritz", LastName = "Green"});
}
private void PrintButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(CurrentHouse.Owner + ":" + CurrentHouse.Number);
}
}
public class House
{
public string Owner { get; set; }
public string Number { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
Maybe someone has an idea,
Christian