2013-02-15 64 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;