【发布时间】:2020-10-14 07:08:53
【问题描述】:
我有一个绑定到ObservableCollection 的ComboBox(CBaddress)。
XAML
<ComboBox
x:Name="CBaddress"
Height="23"
Margin="80,75,423,0"
VerticalAlignment="Top"
ItemTemplate="{StaticResource AddressTemplate}"
ItemsSource="{Binding}"
/>
<DataTemplate x:Key="AddressTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Width="50" Text="{Binding Path=ID_}" />
<TextBlock Width="100" Text="{Binding Path=Address_}" />
<TextBlock Width="30" Text="{Binding Path=HouseNumber_}" />
<TextBlock Width="40" Text="{Binding Path=PostalCode_}" />
<TextBlock Width="150" Text="{Binding Path=State_}" />
</StackPanel>
</DataTemplate>
ObservableCollection 由 class(地址)组成。
class Address
{
public int ID_ { get; set; }
public string Country_ { get; set; }
public string State_ { get; set; }
public int PostalCode_ { get; set; }
public string Address_ { get; set; }
public int HouseNumber_ { get; set; }
}
当我的程序启动时,它会从数据库中加载所有值,并可以在ComboBox 中完美显示所有值:
CBaddress.DataContext = database.SelectAllAddresses();
但是我如何获得这些值?使用CBaddress.Text 我只得到这个输出:
MySQL_WPF.classes.Address
是否可以得到纯文本,也显示在ComboBox中?
如果我能从选定的值中得到某个值,那就最好了,比如ID_。
【问题讨论】:
-
ComboBox中应该显示哪个属性?您所说的“我如何获得价值?”是什么意思?你想获取哪些值,选中的项,所有的项? -
XAML DataTemplate 中显示的属性,没关系。我的问题是,我如何使用选定的值。喜欢是否可以只获取所选值的 ID_
标签: c# wpf class combobox observablecollection