views:

164

answers:

1

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<Person> _PersonList = new List<Person>();        
    public List<Person> 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

A: 

If I understand your question, you want to take the last name of the currently selected Person in the ComboBox and set it's value to the CurrentHouse.Owner property. If so, review the following and see if it makes sense. Specifically, take a look at the setting of the SelectedValue and SelectedValuePath attributes of the ComboBox.

Code Behind

public partial class MultiColumnCombobox : Window
{
    public List<Person> PersonList { get; set; }

    public House CurrentHouse { get; set; }

    public MultiColumnCombobox()
    {
        InitializeComponent();

        PersonList = new List<Person>
                         {
                             new Person
                                 {
                                     FirstName = "Peter",
                                     LastName = "Smith"
                                 },
                             new Person
                                 {
                                     FirstName = "John",
                                     LastName = "Meyer"
                                 },
                             new Person
                                 {
                                     FirstName = "Fritz",
                                     LastName = "Green"
                                 }
                         };

        CurrentHouse = new House
                           {
                               Owner = "Green",
                               Number = "11"
                           };

        this.DataContext = this;

        ShowCurrentHouseInfo();
    }

    private void OnComboBoxChanged( object sender, SelectionChangedEventArgs e )
    {
        ShowCurrentHouseInfo();
    }

    private void ShowCurrentHouseInfo()
    {
        MyTextBlock.Text = string.Format(
            "CurrentHouse.Owner: {0} : CurrentHouse.Number: {1}",
            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; }
}

XAML

<StackPanel>
    <ComboBox
        Margin="10"
        Name="ComboBox1"
        VerticalAlignment="Top"        
        SelectedValue="{Binding CurrentHouse.Owner}"
        SelectedValuePath="LastName"
        SelectionChanged="OnComboBoxChanged"
        ItemsSource="{Binding PersonList}">
        <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>

    <TextBlock x:Name="MyTextBlock" Margin="10" />

</StackPanel>
Metro Smurf