我想讓我的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