如何使用 MVVM Light 绑定和 XAML 填充 ComboBox?
How to fil a ComboBox with MVVM Light binding and XAML?
我有一个列表,其中包含我想在 ComboBox 中显示的项目,但结果是我看不到文本,但是这个:
App1.Data.Models.Test
App1.Data.Models.Test
App1.Data.Models.Test
我真的不知道如何显示正确的文本。
模型测试有 2 个属性 ID 和名称。
<ComboBox Grid.Column="1"
Grid.Row="3"
Margin="10"
ItemsSource="{Binding TestList}" />
我需要像使用 ListView 一样使用 DataTemplate 吗?
您需要从App1.Data.Models.Test
定义您想要显示的属性DisplayMemberPath
。
或者覆盖 ToString()
<ComboBox Grid.Column="1"
Grid.Row="3"
Margin="10"
DisplayMemberPath = "Name"
ItemsSource="{Binding TestList}" />
ComboBox
使用其项目的 ToString()
方法来显示它们。
要么覆盖 App1.Data.Models.Test.ToString()
,要么选择 App1.Data.Models.Test
中的特定 属性 来显示,比方说 Name
:
<ComboBox
Grid.Column="1"
Grid.Row="3"
Margin="10"
ItemsSource="{Binding TestList}"
DisplayMemberPath="Name" />
我有一个列表,其中包含我想在 ComboBox 中显示的项目,但结果是我看不到文本,但是这个:
App1.Data.Models.Test
App1.Data.Models.Test
App1.Data.Models.Test
我真的不知道如何显示正确的文本。
模型测试有 2 个属性 ID 和名称。
<ComboBox Grid.Column="1"
Grid.Row="3"
Margin="10"
ItemsSource="{Binding TestList}" />
我需要像使用 ListView 一样使用 DataTemplate 吗?
您需要从App1.Data.Models.Test
定义您想要显示的属性DisplayMemberPath
。
或者覆盖 ToString()
<ComboBox Grid.Column="1"
Grid.Row="3"
Margin="10"
DisplayMemberPath = "Name"
ItemsSource="{Binding TestList}" />
ComboBox
使用其项目的 ToString()
方法来显示它们。
要么覆盖 App1.Data.Models.Test.ToString()
,要么选择 App1.Data.Models.Test
中的特定 属性 来显示,比方说 Name
:
<ComboBox
Grid.Column="1"
Grid.Row="3"
Margin="10"
ItemsSource="{Binding TestList}"
DisplayMemberPath="Name" />