Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: ComboBox displays "(namespace).(classname)" instead of data

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>
like image 636
Edward Tanguay Avatar asked Feb 02 '09 15:02

Edward Tanguay


People also ask

What is combobox in WPF?

WPF - Combobox. A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item.

How to add XAML tags for comboboxes and textboxes in WPF project?

Let’s create a new WPF project with the name WPFComboBoxControl. Drag two comboboxes and two textboxes from a toolbox and set the following properties in the properties window. Now switch to XAML window in which you will see the XAML tags for comboboxes and textboxes.

How does the combobox work?

The ComboBox control presents users with a list of options. The list is shown and hidden as the control expands and collapses. In its default state, the list is collapsed, displaying only one choice. The user clicks a button to see the complete list of options. The following illustration shows a ComboBox in different states.

What is combobox in Salesforce?

A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item.


3 Answers

Just add this in you ComboBox tag (xaml code)

TextSearch.TextPath ="Title"

ToString() modification affects all future cases you need it, I don't recommend you to do that.

See that example: http://simplesample.site90.com/wpf_combobox.php

like image 187
Rafa Avatar answered Sep 28 '22 00:09

Rafa


You can override the ToString method of Contact, or you can define a DataTemplate in WPF.

xmlns:dpwpf="... namespace of dpwpf"

<Window.Resources>
    <DataTemplate DataType="{x:Type dpwpf:Contact}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding LastName}">
            <TextBlock Text=" ">
            <TextBlock Text="{Binding FirstName}">
        </StackPanel>
    </DataTemplate>
</Window.Resources>

This allows you to define how a Contact object will appear anywhere in the window. If you want to limit this to the ComboBox you can add the DataTemplate just to the ComboBox resources.

like image 43
Cameron MacFarland Avatar answered Sep 27 '22 23:09

Cameron MacFarland


you need to use DisplayMember="TheValue"

you can add this in the XAML or in the CLR also note your binding in cose and not through the XAML. i think this relates to a prvious question of yours. which implements the binding through ObjectDataProvider use that example and add the DisplayMemeber="memeber" in the XAML

HTH, Eric,

like image 41
user61477 Avatar answered Sep 27 '22 23:09

user61477