简体   繁体   English

组合框显示项目

[英]Combobox Display items

Does the ItemsSource property not bind to the item? ItemsSource属性不绑定到项目吗?

XAML : XAML:

 <ComboBox  Name="cmbUserName" Height="30"  
          VerticalAlignment="Top" Margin="10,64,614,0"/>

 <ComboBox Name="cmbAcessTime" Height="30"  
          VerticalAlignment="Top" Margin="354,64,288,0"/>

C# : C# :

DMSService.DMSService service = new DMSService.DMSService();
var logList = service.GetLogDetails();
var UserNames = (from A in logList orderby A.FirstName select new UserName { Users = A.FirstName + " " + A.SurName, ID = Convert.ToInt32(A.Id) }).Distinct();
var loginDate = (from A in logList select A.LogInTime.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)).Distinct();

cmbUserName.ItemsSource = UserNames;
cmbAccessDate.ItemsSource = loginDate;

在此处输入图片说明

I want to add these usernames in Combobox and set ID. 我想在组合框中添加这些用户名并设置ID。 Then, pass ID where combobox on change. 然后,在组合框更改的地方传递ID。

在此处输入图片说明

You need to set ComboBox property SelectedValuePath and DisplayMemberPath from your datasource. 您需要从数据源设置ComboBox属性SelectedValuePath和DisplayMemberPath。 Please check the below XAML: 请检查以下XAML:

    <ComboBox  Name="cmbUserName" Height="30"  
              VerticalAlignment="Top" Margin="10,64,614,0"
              SelectedValuePath="Id" DisplayMemberPath="usernames" />

     <ComboBox Name="cmbAcessTime" Height="30"  
              VerticalAlignment="Top" Margin="354,64,288,0"
              SelectedValuePath="Id" DisplayMemberPath="usernames" />

You can get selected value of ComboBox using the below code and pass value where you want. 您可以使用以下代码获取ComboBox的选定值,并在所需的位置传递值。

var selectedHPQ = (int)cmbUserName.SelectionBoxItem;

Thanks 谢谢

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM