简体   繁体   English

从 Combobox 中获取选定的值到 WPF 中的 Datagrid

[英]Get selected value from Combobox to Datagrid in WPF

I have the following ViewModel in my code:我的代码中有以下 ViewModel:

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中的 Combobox 的方式。

<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 .我想在Datagrid中显示Combobox选定值
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.如果您在 ViewModel 中添加“SelectedProduct”属性并将文本块绑定到该属性。 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.将 ComboBox XAML 更改为 select 当前项到 SelectedProduct 属性。 Then bind the TextBlocks to the selected products properties.然后将 TextBlocks 绑定到选定的产品属性。

        <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.我猜你已经在 MainWindow 中有 ViewModel 作为 DataContext。

The PropertyChanged event is what triggers the binding to update the textblocks. PropertyChanged 事件触发绑定以更新文本块。


EDIT: DataGrid编辑:数据网格

The DataGrid want a collection as the source. DataGrid 想要一个集合作为源。 You can add a collection in the view model that gets the selected product and then bind the DataGrid to that collection.您可以在获取所选产品的视图 model 中添加一个集合,然后将 DataGrid 绑定到该集合。

In The ViewModel class:在 ViewModel class 中:

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

Add the selected product in the SelectedProduct property:在 SelectedProduct 属性中添加选定的产品:

    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在 XAML 中添加 DataGrid

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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