【问题标题】:WPF - How to get selected value binded to a table modelWPF - 如何将选定的值绑定到表模型
【发布时间】:2015-08-08 18:35:24
【问题描述】:

我有一个组合框类别,它绑定到基于tbl_CategoryObservableCollection Categories,具有两个属性CategoryNameCategoryDescription。现在我想将ComboBoxSelectedValue 添加到产品表属性Prod_Category

在我的构造函数中:

cb.DataContext = Categories;
this.DataContext = new tbl_Product();

组合框 xaml:

<Combobox x:Name="cb" ItemSource="{Binding Categories}" DisplayMemberPath="CategoryName" SelectedValuePath="CategoryName" SelectedValue="{Binding Prod_Category,Mode=TwoWay}"/>

在我的保存产品活动中:

tbl_Product prod = (tbl_Product)this.DataContext;
DataOperations.AddProduct(prod);

即使在完成所有这些操作之后,我也会将 Prod_Category 变为 null

【问题讨论】:

    标签: wpf data-binding linq-to-sql combobox selectedvalue


    【解决方案1】:

    您应该使用 SelectedItem 而不是 SelectedValue,请参阅this

    除了你愿意做的事情不是很清楚,我已经尝试根据我的理解实现你所要求的

     public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private ObservableCollection<Category> _categories;
        public ObservableCollection<Category> Categories
        {
            get
            {
                return _categories;
            }
    
            set
            {
                if (_categories == value)
                {
                    return;
                }
    
                _categories = value;
                OnPropertyChanged();
            }
        }
        private Category _prod_Category ;
        public Category Prod_Category
        {
            get
            {
                return _prod_Category;
            }
    
            set
            {
                if (_prod_Category == value)
                {
                    return;
                }
    
                _prod_Category = value;
                OnPropertyChanged();
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            Categories=new ObservableCollection<Category>()
            {
                new Category()
                {
                    CategoryName = "Name1",
                    CategoryDescription = "Desc1"
                },new Category()
                {
                    CategoryName = "Name2",
                    CategoryDescription = "Desc2"
                }
            };
        }
    
        public void SaveButton_Click(object sender, RoutedEventArgs routedEventArgs)
        {
            if (Prod_Category!=null)
            {
                //add it to whatever you want
    
            }
        }
    
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    public class Category
    {
        public String CategoryName { get; set; }
        public String CategoryDescription { get; set; }
    }
    

    和 xaml

     <StackPanel>
        <ComboBox x:Name="cb" ItemsSource="{Binding Categories}" DisplayMemberPath="CategoryName" SelectedValuePath="CategoryName" SelectedItem="{Binding Prod_Category,Mode=TwoWay}"/>
        <Button Content="Save" Click="SaveButton_Click"></Button>
    </StackPanel>
    

    您应该考虑实现INotifyPropertyChanged 接口,以在绑定属性之一发生任何更改时通知 UI

    【讨论】:

      【解决方案2】:

      除了@samthedev 提供的答案,我还建议您将DataContext 的分配从:

      tbl_Product prod = (tbl_Product)this.DataContext;
      DataOperations.AddProduct(prod);
      

      tbl_Product prod = this.DataContext as tbl_Product;
      if (tbl_Product != null)
      {
          DataOperations.AddProduct(prod);
      }
      

      这可以防止 DataContext 的任何更改意外绑定到不同的对象并导致未处理的异常,因为 DataContext 确实将 tbl_Product 作为其基本类型,由于 DataContext 继承,这种情况比您在 WPF 中意识到的更频繁地发生有人在更高的层次上改变了一些东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-04
        • 1970-01-01
        • 2020-01-19
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多