简体   繁体   English

WPF 获取绑定到 observablecollection 的选定组合框值

[英]WPF get selected Combobox value which is bound to observablecollection

I have a ComboBox (CBaddress) which is bound to an ObservableCollection .我有一个绑定到ObservableCollectionComboBox (CBaddress)。

XAML 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>

The ObservableCollection consists of a class (Address). 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; }
}

When my program starts it loads all values from a database and can display all values perfectly in the ComboBox :当我的程序启动时,它会从数据库中加载所有值,并且可以在ComboBox完美显示所有值:

CBaddress.DataContext = database.SelectAllAddresses();

But how do I get the values?但是我如何获得这些值? With CBaddress.Text I get only this output :使用CBaddress.Text我只得到这个输出:

MySQL_WPF.classes.Address MySQL_WPF.classes.Address

Is it possible to get the plain text, which is also displayed in the ComboBox ?是否可以获取也显示在ComboBox的纯文本?

It would be best if I could get a certain value from the selected value, like the ID_ .如果我能从选定的值中获得某个值,比如ID_ ,那将是最好的。

If you want to get the selected item, access it with the SelectedItem property on ComboBox .如果要获取所选项目,请使用ComboBox上的SelectedItem属性访问它。

var selectedID = ((Address)CBaddress.SelectedItem).ID_ ;

The SelectedItem property is of type object , so you need to cast it to your data type Address . SelectedItem属性属于object类型,因此您需要将其强制转换为您的数据类型Address Then you can access any of its properties as usual.然后您可以像往常一样访问它的任何属性。

If you are working in an MVVM scenario, you would bind the SelectedItem to a property on your view model, eg SelectedAddress .如果您在 MVVM 场景中工作,您会将SelectedItem绑定到视图模型上的属性,例如SelectedAddress

<ComboBox ...
          ItemsSource="{Binding}"
          SelectedItem={Binding SelectedAddress}"/>
private Address _selectedAddress;
public Address SelectedAddress
{
   get => _selectedAddress;
   set
   {
      if (_selectedAddress == value)
         return;

      _selectedAddress = value;
      OnPropertyChanged();
   }
}

Then you could access any property the same way, eg:然后您可以以相同的方式访问任何属性,例如:

var selectedID = SelectedAddress.ID_;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取wpf组合框选择的值 - Get wpf combobox selected value 数据绑定的WPF ComboBox不显示选定的值 - Data-bound WPF ComboBox not displaying selected value 从 Combobox 中获取选定的值到 WPF 中的 Datagrid - Get selected value from Combobox to Datagrid in WPF 试图根据另一个ComboBox值将绑定的ObservableCollection筛选到组合框,但无法正常工作 - Trying to Filter a bound ObservableCollection to a combobox based on another ComboBox Value not working 绑定到 ObservableCollection (WPF) 时无法更改 ComboBox 选择 - Can't change ComboBox selection when bound to ObservableCollection (WPF) WPF在组合框中选择的值 - WPF selected value in ComboBox 从绑定到ObservableDictionary的ComboBox的值中获取Selected Field - Get Selected Field off a value on a ComboBox bound to ObservableDictionary 从上下文菜单中获取所选菜单项的名称(或索引),该菜单项是通过绑定到ObservableCollection的ItemsSource动态生成的 - Get name(or index) of selected menu item from context menu, which was dynamically generated via ItemsSource bound to a ObservableCollection WPF ComboBox到ObservableCollection绑定 - WPF ComboBox to ObservableCollection binding 如何保存在wpf中绑定到ObservableCollection的tabcontrol项的选项卡顺序? - How to save the tab order of tabcontrol items which are bound to an ObservableCollection in wpf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM