2013-02-15 23 views
0

我有一个静态值很少的组合框。如何绑定具有静态值的组合框中的数据表值?

<ComboBox Name="cmbBoxField" Grid.Column="4" Grid.Row="2" Style="{StaticResource comboBoxStyleFixedWidth}" ItemsSource="{Binding}" ></ComboBox> 

MVVMModle1.cmbBoxField.Items.Add(new CustomComboBoxItem("Text Box", "0")); 
MVVMModle1.cmbBoxFieldType.Items.Add(new CustomComboBoxItem("Pick List", "1")); 
MVVMModle1.cmbBoxFieldType.Items.Add(new CustomComboBoxItem("Check Box", "2")); 
MVVMModle1.cmbBoxFieldType.Items.Add(new CustomComboBoxItem("Radio Button", "3")); 

当我在数据库表中保存数据时,它正在保存。

((CustomComboBoxItem)(MVVMModle1.cmbBoxField.SelectedValue)).Value.ToString(); 

现在,当我试图编辑我的窗体并再次将值绑定到组合框时,它不显示值。

MVVMModle1.cmbBoxField.SelectedValue = dtDataList.Rows[0]["ControlList"].ToString().Trim(); 

有人请在这帮我。如何将选定的值绑定到组合框?

回答

1

有相当与你的代码在这里有几个问题:

  • 您的ItemsControl.ItemsSource属性设置为默认绑定(绑定到当前数据上下文),这是不正确,除非DataContext是任何实现IEnumerable的类型,它可能不是。
  • 如果这是正确的,因为DataContext是,例如,一个ObservableCollection<T>,那么你仍然有一个问题,因为你是静态地将项目添加到ComboBox,而不是无论ItemsSource是。
  • 此外,您要添加的项目类型是CustomComboBoxItem,我将假定它继承自ComboBoxItem。无论哪种方式,您不能说SelectedValue是一些字符串,因为ComboBox中的值不是字符串。
  • 你真的不应该有一个CustomComboBoxItem的集合,而是一个自定义的类,它本身就是它自己的ViewModel。

既然是有人说,这里是一个建议的解决问题的方法:

<ComboBox ItemsSource="{Binding Path=MyCollection}" 
      SelectedValue="{Binding Path=MySelectedString}" 
      SelectedValuePath="StringProp" /> 

public class CustomComboBoxItem : ComboBoxItem 
{ 
    // Not sure what the property name is... 
    public string StringProp { get; set; } 

    ... 
} 

// I'm assuming you don't have a separate ViewModel class and you're using 
// the actual window/page as your ViewModel (which you shouldn't do...) 
public class MyWPFWindow : Window, INotifyPropertyChanged 
{ 
    public MyWPFWindow() 
    { 
     MyCollection = new ObservableCollection<CustomComboBoxItem>(); 

     // Add values somewhere in code, doesn't have to be here...    
     MyCollection.Add(new CustomComboBoxItem("Text Box", "0")); 
     etc ... 

     InitializeComponent(); 
    } 

    public ObservableCollection<CustomComboBoxItem> MyCollection 
    { 
     get; 
     private set; 
    } 

    private string _mySelectedString; 
    public string MySelectedString 
    { 
     get { return _mySelectedString; } 
     set 
     { 
      if (String.Equals(value, _mySelectedString)) return; 

      _mySelectedString = value; 
      RaisePropertyChanged("MySelectedString"); 
     } 
    } 

    public void GetStringFromDb() 
    { 
     // ... 

     MySelectedString = dtDataList.Rows[0]["ControlList"].ToString().Trim(); 
    } 
} 

你可以或者不执行INotifyPropertyChanged并使用您的MySelectedString财产DependencyProperty,但使用INPC是首选方式。无论如何,这应该给你足够的信息,以了解朝向哪个方向......

TL; DR;

  • 利用绑定到ObservableCollection<T>(为此创建一个属性)。
  • 将您的项目添加到ObservableCollection<T>
  • ItemsSource绑定到您创建的新集合属性。
  • SelectedValue绑定到您创建的某个字符串属性(利用INPC)。
  • SelectedValuePath设置为CustomComboBoxItem的字符串属性名称的路径。
0

您能使用cmbBoxField.DataBoundItem()吗?如果没有将来源从选定的值定位,即获取ID,则再次查询源以获取数据。

(CustomComboBoxItem)MVVMModle1.cmbBoxField.DataBoundItem(); 

当您绑定数据源是简单的做这样的:

私人列表GetItems(){

List<CustomComboBoxItem> items = new List<CustomComboBoxItem>(); 
    items.Add(new CustomComboBoxItem() {Prop1 = "Text Box", Prop2 = "0"}); 
    //...and so on 
    return items; 
} 

然后在你的主代码:

List<CustomComboBoxItem> items = this.GetItems(); 

MVVMModle1.cmbBoxField.DisplayMember = Prop1; 
MVVMModle1.cmbBoxField.ValueMember = Prop2; 
MVVMModle1.cmbBoxField.DataSource = items; 

然后这将允许您选择的值工作,或者通过索引,值或文本进行选择

var selected = dtDataList.Rows[0]["ControlList"].ToString().Trim(); 

MVVMModle1.cmbBoxField.SelectedValue = selected; 
相关问题