C# 使用Id值绑定组合框名称
我有一个组合框控件。我正在从数据库中获取组合框值。我有身份证和名字。但我只绑定组合框中的名称。我想要的是, 1.如果我在组合框中选择任何名称,我需要在数据库中保存相应的Id。现在我可以绑定数据库中的值,并在Combobox中显示名称。但当我在combobox中选择任何值并尝试保存means时,它在ID上显示空值。这是我的代码。请帮我找到一些解决办法。 Xaml: 注意:MasterRentalType是我获取值的表格。这里有ID和Name值C# 使用Id值绑定组合框名称,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,我有一个组合框控件。我正在从数据库中获取组合框值。我有身份证和名字。但我只绑定组合框中的名称。我想要的是, 1.如果我在组合框中选择任何名称,我需要在数据库中保存相应的Id。现在我可以绑定数据库中的值,并在Combobox中显示名称。但当我在combobox中选择任何值并尝试保存means时,它在ID上显示空值。这是我的代码。请帮我找到一些解决办法。 Xaml: 注意:MasterRentalType是我获取值的表格。这里有ID和Name值 public class MasterRental
public class MasterRentalType : EntityBase
{
public string RentalTypeId {get; set;}
public string RentalTypeName {get; set;}
}
如何绑定和保存id值?您的问题是因为您将数据绑定到了
组合框中。SelectedValue
属性绑定到您的MasterRentalType
属性,我假设该属性的类型为MasterRentalType
,但随后您将SelectedValuePath
属性设置为RentalType
。因此,您是说*让SelectedValue
使用string RentalTypeId
属性,然后将MasterRentalType
数据绑定到它
有许多解决方案。纠正您的示例,您应该尝试以下方法:
<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0"
SelectedValue="{Binding MasterRentalType.RentalTypeId}"
DisplayMemberPath="RentalTypeName"
SelectedValuePath="RentalTypeId" />
或者,您也可以这样做:
<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0"
SelectedItem="{Binding MasterRentalType}"
DisplayMemberPath="RentalTypeName" />
要了解更多有关差异的信息,请查看MSDN上的页面。您的问题是因为您将数据绑定到了组合框中。SelectedValue属性到您的
MasterRentalType
属性,我假设该属性的类型为MasterRentalType
,然后将SelectedValuePath
属性设置为RentalTypeId
。因此,您是说*让SelectedValue
使用string RentalTypeId
属性,然后将MasterRentalType
数据绑定到它
有许多解决方案。纠正您的示例,您应该尝试以下方法:
<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0"
SelectedValue="{Binding MasterRentalType.RentalTypeId}"
DisplayMemberPath="RentalTypeName"
SelectedValuePath="RentalTypeId" />
或者,您也可以这样做:
<ComboBox x:Name="cb_rentaltype" HorizontalAlignment="Left" Margin="150,5,0,0"
SelectedItem="{Binding MasterRentalType}"
DisplayMemberPath="RentalTypeName" />
要了解更多有关差异的信息,请查看MSDN上的页面。有几种方法可以实现这一点。其中一项实施如下: xaml应该如下所示:
<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>
模型类:
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
}
每当用户更改选择时,SelectedMasterRentalType
都将更新。在SelectedMasterRentalType
中,您可以同时获得RentalTypeId
和RentalTypeName
。如果您遵循正确的绑定,您的模型将始终更新;这就是WPF的本质
希望这会有所帮助。有几种方法可以实现这一点。其中一项实施如下: xaml应该如下所示:
<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>
模型类:
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
}
每当用户更改选择时,SelectedMasterRentalType
都将更新。在SelectedMasterRentalType
中,您可以同时获得RentalTypeId
和RentalTypeName
。如果您遵循正确的绑定,您的模型将始终更新;这就是WPF的本质
希望这会有所帮助。如何再次绑定所选名称?我想匹配Id并在此处显示相应的名称,private void FacilityDataBind(){cb_rentaltype.DataContext=??}我无法保存Id值..请再次阅读答案和MSDN上的链接页面。。。我不再重复了,我已经给了你一个有效的答案。再次阅读它和MSDN上的链接页面。你所要做的就是阅读、复制和粘贴。。。我不会重复我自己的话。我怎样才能再次绑定所选的名称?我想匹配Id并在此处显示相应的名称,private void FacilityDataBind(){cb_rentaltype.DataContext=??}我无法保存Id值..请再次阅读答案和MSDN上的链接页面。。。我不再重复了,我已经给了你一个有效的答案。再次阅读它和MSDN上的链接页面。你所要做的就是阅读、复制和粘贴。。。我不会重复我的话。
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;
}