Search code examples
wpfdata-bindinglinq-to-sqlcomboboxselectedvalue

WPF - How to get selected value binded to a table model


I have a combo box category that is binded to an ObservableCollection Categories based on tbl_Category with two properties CategoryName and CategoryDescription. Now I want to add the SelectedValue of the ComboBox to product table property Prod_Category

In my constructor :

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

Combo box xaml :

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

In my save product event :

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

I get Prod_Category to null even after doing all this.


Solution

  • You should use SelectedItem instead of SelectedValue, refer to this.

    besides that what you are willing to do wasn't so clear, I've tried to implement what you asked for based on my understanding

     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; }
    }
    

    and the 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>
    

    you should consider implementing the INotifyPropertyChanged interface to notify the UI if any changes occurs in one of the binded properties