在示例WPF项目中(参见下面的代码)我有一个ComboBox
控件,它使用DataTemplate
来显示其绑定项(MyItem
个对象的数组)。所有这一切都按预期工作。在ComboBox
上选择一个项目后,它会显示所选项目的完整DataTemplate。我想要发生的是
ComboBox
时,它会显示每个绑定项目的完整模板。ComboBox
关闭),它只显示绑定项目的单个属性,例如Name
对象的MyItem
属性。< / LI>
以下是一些示例代码:
XAML:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication4"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding .}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Description}"/>
<TextBlock Text="{Binding Message}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
代码背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MyItem[]
{
new MyItem() { Name = "Name1", Message="Message 1", Description = "Description 1" },
new MyItem() { Name = "Name2", Message="Message 2", Description = "Description 2" },
new MyItem() { Name = "Name3", Message="Message 3", Description = "Description 3" }
};
}
}
public class MyItem
{
public string Name { get; set; }
public string Description { get; set; }
public string Message { get; set; }
}