我想在一个组合框中显示来自两个数据库列的内容。我想显示“姓氏”,但我不知道如何显示。我正在使用 MVVM 模式在 C# (.NET) 中工作。 “姓名”和“姓氏”是表“tblGuests”中的字段。
提前致谢,
弗拉基米尔
最佳答案
您可以为 ComboBox 创建一个 ItemTemplate,它绑定(bind)到您要显示的所有属性。
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}" Padding="10,0,0,0"/>
<TextBlock Text="{Binding Path=LastName}" Padding="10,0,0,0"/>
</StackPanel>
<DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
您甚至可以创建一个可以重复使用的 UserControl 并使用它:
<PersonView>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}" Padding="10,0,0,0"/>
<TextBlock Text="{Binding Path=LastName}" Padding="10,0,0,0"/>
</StackPanel>
</PersonView>
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<PersonView/>
<DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
关于c# - 在一个组合框中显示数据库中的多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16891503/