简体   繁体   中英

Get selected value from Combobox to Datagrid in WPF

I have the following ViewModel in my code:

public List<Product> ProList { set; get;} = new List<Product>
{
 new Product
 {
   Num = 1,
   CName = "AAA",
   CAdd = "Maple",
   CPh = 012345
 },
 new Product
 {
   Num = 2,
   CName = "BBB",
   CAdd = "Moon",
   CPh = 012345
 },
 new Product
 {
   Num = 3,
   CName = "CCC",
   CAdd = "River",
   CPh = 012345
 }
};

This is how I bind data to a Combobox in XAML.

<Combobox Name="MyComboBox" IsEditable="True" Text="Select" ItemsSource="{Binding ProList}" DisplayMemberPath="CName" SelectedValue="CName"/>

I want to display the selected value of the Combobox in the Datagrid .
It should look something like this:

AAA

No    Name     Address  Ph No

1     AAA      Maple    012345

If you add a "SelectedProduct" property in your ViewModel and bind the textblocks to that property. The textboxes will show the data from the selected product.

ViewModel

class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Product _selectedProduct;

    public Product SelectedProduct
    {
        get { return _selectedProduct; }
        set 
        {
            _selectedProduct = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedProduct)));
        }
    }
    public ObservableCollection<Product> ProList { get; } = new ObservableCollection<Product>
    {
        new Product
     {
       Num = 1,
...

Change you ComboBox XAML to select current item to the SelectedProduct property. Then bind the TextBlocks to the selected products properties.

        <ComboBox Name="MyComboBox" IsEditable="True" Text="Select" ItemsSource="{Binding ProList}" DisplayMemberPath="CName"
                  SelectedItem="{Binding SelectedProduct}"/>
        <TextBlock Text="{Binding SelectedProduct.CName}"/>

I'll guess you already have the ViewModel as DataContext in the MainWindow.

The PropertyChanged event is what triggers the binding to update the textblocks.


EDIT: DataGrid

The DataGrid want a collection as the source. You can add a collection in the view model that gets the selected product and then bind the DataGrid to that collection.

In The ViewModel class:

    public ObservableCollection<Product> SelectedProducts { get; } = new ObservableCollection<Product>();

Add the selected product in the SelectedProduct property:

    public Product SelectedProduct
    {
        get { return _selectedProduct; }
        set 
        {
            _selectedProduct = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedProduct)));
            SelectedProducts.Clear();
            SelectedProducts.Add(value);
        }
    }

Add the DataGrid in XAML

        <DataGrid ItemsSource="{Binding SelectedProducts}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM