0

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.

perror
  • 7,071
  • 16
  • 58
  • 85
Ivan D'souza
  • 105
  • 2
  • 9

2 Answers2

2

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

Community
  • 1
  • 1
SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
0

In addition to @samthedev 's provided answer, I would also recommend you change your assignment of DataContext from:

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

to

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

This prevents any change of DataContext being accidentally bound to a different object and causing an unhandled exception due to the fact that DataContext does have tbl_Product as its base-type which can happen more often than you realise in WPF due to DataContext inheritance when someone changes something at a higher level.

netniV
  • 2,328
  • 1
  • 15
  • 24