我想让我的ComboBox
在XAML中绑定到我的List
代码后面的自定义对象集合。WPF:ComboBox显示“(namespace)。(classname)”而不是数据
当前ComboBox
列出了每个条目dpwpf.Contact
这是我的{namespace}.{classname}
。
我需要在XAML中放置什么来告诉它列出来,例如:姓氏+名字?
我知道这是类似{Binding Path=... Value=...}
但我无法得到它。
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>
代码背后:
namespace dpwpf
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
StoreDB db = new StoreDB();
List<Contact> contacts = db.GetContacts()
theContactList.ItemsSource = contacts.ToList();
}
}
}
答:
<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>
的[WPF IsEditable =填充对象真正组合框显示的ToString()作为所选择的项目]可能的复制(https://stackoverflow.com/questions/1844156/wpf- iseditable-true-combobox-filled-with-objects-displays-the-tostring-as-the) – 2017-10-26 09:48:39