使用Id值绑定组合框名称 [英] Binding Combobox Name using Id value
问题描述
我有一个组合框控件。我从数据库中获取组合框值。我有Id和名字。但我只绑定在组合框中的名称。我想要的是,
1.如果我在Combobox中选择任何名称,我需要保存对应的ID在我的数据库。现在我可以绑定来自数据库的值和在Combobox中显示名称。但是当我在combobox中选择任何值并尝试保存方法时,它在ID上显示null值。这里是我的代码。请帮我找到一些解决方案。
Xaml:
I have a combobox control. I am getting combobox values from Database. There I have Id and Name. But I am binding only Name in the combobox. what i want is, 1. If I select any name in the Combobox, I need to save the Corresponding Id in my Database. Now I am able to bind the value from database and Display the Name in Combobox. but when I select any value in combobox and try to save means, it shows null value on the ID. Here is my code. Please help me to find some solution. Xaml:
<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0" VerticalAlignment="Top" Height="35" Width="200"
SelectedValue="{Binding MasterRentalType}"
DisplayMemberPath="RentalTypeName"
SelectedValuePath="RentalTypeId" />
我的代码背后:
var status = new MasterRentalType();
List<MasterRentalType> listRentalType =
status.Get<MasterRentalType>() as
List<MasterRentalType>;
cb_rentaltype.ItemsSource = listRentalType;
,我想将Id绑定到数据上下文。这里是代码,
and i want to bind the Id to the Data context. here is the code,
private void FacilityDataBind()
{
cb_rentaltype.DataContext = ??
}
注意:MasterRentalType是我获取值的表。我有ID和名称值。
Note: MasterRentalType is the Table where i get the Values. There I have ID and Name values.
public class MasterRentalType : EntityBase
{
public string RentalTypeId {get; set;}
public string RentalTypeName {get; set;}
}
如何绑定和保存id值? >
how can I bind and save the id value?
推荐答案
有几种方法可以实现这一点。其中一个实现如下:
There are several ways to achieve this. One of them is implemented below:
xaml应该如下所示:
The xaml should look like this:
<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0"
ItemsSource="{Binding MasterRentalTypeColl, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItem="{Binding SelectedMasterRentalType, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="RentalTypeName">
</ComboBox>
MasterRentalType
class:
public class MasterRentalType : EntityBase, INotifyPropertyChanged
{
private string _RentalTypeId;
private string _RentalTypeName;
public string RentalTypeId
{
get { return _RentalTypeId; }
set
{
_RentalTypeId = value;
NotifyPropertyChanged("RentalTypeId");
}
}
public string RentalTypeName
{
get { return _RentalTypeName; }
set
{
_RentalTypeName = value;
NotifyPropertyChanged("RentalTypeName");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
$ b b
模型类:
The model class:
public class MasterRentalModel: INotifyPropertyChanged
{
private MasterRentalType _SelectedMasterRentalType;
private List<MasterRentalType> _MasterRentalTypeColl = new List<MasterRentalType>();
public MasterRentalType SelectedMasterRentalType
{
get { return _SelectedMasterRentalType; }
set
{
_SelectedMasterRentalType = value;
NotifyPropertyChanged("SelectedMasterRentalType");
}
}
public List<MasterRentalType> MasterRentalTypeColl
{
get { return _MasterRentalTypeColl; }
set { _MasterRentalTypeColl = value; }
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
$ b b
在代码隐藏中,你只需要将模型分配给ComboBox的 DataContext
你可以实现这个任何其他函数(这里我已经在 Loaded
事件的窗口
的事件处理程序中实现它) :
And in the code-behind, you just need to assign the model to the DataContext
of you ComboBox; you can implement this any other function (here I have implemented it in the event handler of Loaded
event of my Window
):
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MasterRentalModel masterRentalModel = new MasterRentalModel();
// Fill the list of RentalType here
masterRentalModel.MasterRentalTypeColl.Add(new MasterRentalType() { RentalTypeId = "1", RentalTypeName = "Monthly" });
masterRentalModel.MasterRentalTypeColl.Add(new MasterRentalType() { RentalTypeId = "2", RentalTypeName = "Quarterly" });
masterRentalModel.MasterRentalTypeColl.Add(new MasterRentalType() { RentalTypeId = "1", RentalTypeName = "Yearly" });
cb_rentaltype.DataContext = masterRentalModel;
}
每当用户更改选择时, SelectedMasterRentalType
将被更新。在 SelectedMasterRentalType
中,您可以获得 RentalTypeId
和 RentalTypeName
。如果你遵循正确的绑定,你的模型将总是更新;这是WPF的本质。
Whenever user changes the selection, SelectedMasterRentalType
will be updated. And in the SelectedMasterRentalType
, you can get both RentalTypeId
and RentalTypeName
. If you follow proper binding your model will always be updated; that's the essence of WPF.
希望这将有所帮助。
这篇关于使用Id值绑定组合框名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!