【问题标题】:How to bind datatable value in combobox which has static value?如何在具有静态值的组合框中绑定数据表值?
【发布时间】:2013-02-15 10:30:31
【问题描述】:

我有一个包含少量静态值的 ComboBox。

<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();

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

【问题讨论】:

    标签: c# wpf binding combobox


    【解决方案1】:

    这里的代码有很多问题:

    • 您正在将ItemsControl.ItemsSource 属性设置为默认绑定(绑定到当前数据上下文),这是不正确的,除非DataContext 是实现IEnumerable 的任何类型,它可能不是。李>
    • 如果这是正确的,因为 DataContext 是例如 ObservableCollection&lt;T&gt;,那么您仍然会遇到问题,因为您将项目静态添加到 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&lt;T&gt;(为此创建一个属性)。
    • 将您的项目 (CustomComboBoxItems) 添加到 ObservableCollection&lt;T&gt;
    • ItemsSource 绑定到您创建的新集合属性。
    • SelectedValue 绑定到您创建的某个字符串属性(利用INPC)。
    • SelectedValuePath 设置为CustomComboBoxItem 的字符串属性名称的路径。

    【讨论】:

      【解决方案2】:

      你能用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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多