2014-07-03 57 views
0

我有一个组合框控件。我从数据库中获取组合框值。我有Id和Name。但是我仅在组合框中绑定Name。我想要的是, 1.如果我在Combobox中选择任何名称,我需要在我的数据库中保存对应的ID。现在我可以绑定来自数据库的值并在Combobox中显示名称。但是当我在组合框中选择任何值并尝试保存方法时,它会在ID上显示空值。这是我的代码。请帮我找到一些解决方案。 的XAML:使用Id值绑定Combobox名称

<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绑定到数据上下文。这里是代码,

private void FacilityDataBind() 
    { 
     cb_rentaltype.DataContext = ?? 
    } 

注意:MasterRentalType是我得到值的表。我有ID和名称值。

public class MasterRentalType : EntityBase 
    { 
     public string RentalTypeId {get; set;} 
     public string RentalTypeName {get; set;}  
    } 

我该如何绑定并保存id值?

回答

1

你的问题造成的,因为你有数据绑定的ComboBox.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" /> 

要了解更多有关分歧,请大家看看How to: Use SelectedValue, SelectedValuePath, and SelectedItem页面上MSDN。

+0

我如何再次绑定选定的名称?我想匹配Id并在此处显示相应的名称,private void FacilityDataBind() { cb_rentaltype.DataContext = ?? }我无法保存Id值.. – JohnPaul

+0

再次阅读MSDN上的答案和链接页面...我不打算重复自己。 – Sheridan

+0

我向您提供了一个有效的答案。再次阅读MSDN上的链接页面。所有你需要做的只是一点点阅读和复制和粘贴...我不会重复自己。 – Sheridan

1

有几种方法可以实现这一点。其中之一是如下实施:

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

MasterRentalType类:

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 
} 

模型类:

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 
} 

而在代码隐藏,您只需将模型分配给您的DataContext C omboBox;您可以在此实现任何其他功能(在这里我已经在Loaded事件我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中,您可以同时获得RentalTypeIdRentalTypeName。如果你遵循适当的绑定,你的模型将会被更新;这就是WPF的本质。

希望这会有所帮助。

+0

以TwoWay方式绑定ItemsSource不起作用。 – BendEg