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


I store my combobox in my XML list <> the custom objects behind the code .

The ComboBox list for each entry dpwpf.Contact is currently my {namespace}. {Classname} .

I need to tell XAM in this list, e.g. LastName + FirstName?

I know that this is something like {binding path = ... value = ...} but I can not get it.

XAM:

  & lt; Window x: orbit = "dpwpf.Window1" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "title =" window 1 "height =" 300 "width =" 300 "& gt; & Lt; StackPanel & gt; & Lt; StackPanel & gt; & Lt; TextBlock Text = "Select Contact:" /> & Lt; Combo box name = "The conversational list" /> & Lt; / StackPanel & gt; & Lt; / StackPanel & gt; & Lt; / Window & gt;  

Code behind:

  Namespace DPVPF {Public partial squares window 1: window {public window 1 () {initial group (); Storedi db = new storei (); & Lt; Contacts & gt; Contact = db.GetContacts () theContactList.ItemsSource = contacts.ToList (); }}}  

Answer:

  & lt; Window x: orbit = "dpwpf.Window1" xmlns = "http://schemas.microsoft.com/enfx/ 2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "title =" window 1 "height =" 300 "width =" 300 "xmlns: local =" clr- namespace: dpwpf "& gt; & Lt; Window.Resources & gt; & Lt; DataTemplate DataType = "{x: type local: contact}" & gt; & Lt; StackPanel Orientation = "Horizontal" & gt; & Lt; Text block text = "{binding final name}" /> & Lt; Text block text = "" /> & Lt; TextBlock Text = "{Binding FirstName}" /> & Lt; / StackPanel & gt; & Lt; / DataTemplate & gt; & Lt; /Window.Resources> & Lt; StackPanel & gt; & Lt; Stackpayel margin = "10" & gt; & Lt; Text block text = "contact name:" foreground = "# 555" /> & Lt; Text box name = "theName" /> & Lt; / StackPanel & gt; & Lt; StackPanel & gt; & Lt; TextBlock Text = "Select Contact:" /> & Lt; Combo box name = "The conversational list" /> & Lt; / StackPanel & gt; & Lt; / StackPanel & gt; & Lt; / Window & gt;    

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

  xmlns: dpwpf = "... dpwpf namespace" & lt; window. Resources & gt; & Lt; DataTemplate DataType = "{x: Type DPWF: Contact}" & gt; & Lt; StackPanel Orientation = "Horizontal" & gt; & Lt; Text block text = "{binding final name}" & gt; & Lt; Text block text = "" & gt; & Lt; TextBlock Text = "{Binding FirstName}" & gt; & Lt; / StackPanel & gt; & Lt; / DataTemplate & gt; & Lt; /Window.Resources>  

This allows you to define how a contact object will appear anywhere in the window. If you want to limit it to combobox , then you can add DataTemplate to the combobox resources only.

See also by Bia Stolnitz


Comments