【问题标题】:How to read a list of values from column in a datatable which has a data from a xml file to a combo box in datagrid?如何从数据表中的列中读取值列表,其中包含来自 xml 文件的数据到数据网格中的组合框?
【发布时间】:2016-09-29 06:27:10
【问题描述】:

我有以下 xml 文件

<WG>
  <WGT>
  <TName>tanme1</TName> 
  <Fname>fname1</Fname> 
  <Product>Product1</Product> 
  <Product>Product2 </Product> 
  </WGT>
  <WGT>
  <TName>tanme2</TName> 
  <Fname>fname2</Fname> 
  </WGT>
  <WGT>
  <TName>tanme1</TName> 
  <Fname>fname1</Fname> 
  <Product>Product1</Product> 
  <Product>Product2 </Product> 
  <Product>Product3 </Product> 
  <Product>Product4 </Product> 
  </WGT>
</WG>

我正在尝试将其绑定到视图中的数据网格(xaml 代码)

下面是 ViewModel 类中的代码 sn-p。

private DataTable dt;
public DataTable DT
       {

           get
           {
               return dt;


           }
           set
           {
               dt = value;
               NotifyPropertyChanged("DT");
           }
       }

XmlReader xmlFile = XmlReader.Create("C:/Wafers/WGen.xml", new XmlReaderSettings());

DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
dt = ds.Tables[0];

运行 wpf 项目时,在视图中我只能看到“TName”和“Fname”字段和值。

我想通过绑定在数据网格的组合框中显示产品值列表。 如何从 viewmodel 类中的数据表 dt 中将所有产品值检索到列表中,以便我可以将其绑定到视图中?

【问题讨论】:

  • 梦珠,我已经查过了。在我的情况下它不起作用。

标签: c# wpf xaml mvvm data-binding


【解决方案1】:

创建一个 DataGridTemplateColumn:

 <DataGridTemplateColumn Header="Product" Width="Auto" MinWidth="120">
                <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate DataType="YourModel">
                        <ComboBox
                            ItemsSource="{Binding Path=Products}"                                
                            SelectedValuePath="Key" 
                            DisplayMemberPath="Key"
                            SelectedItem="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

                            />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

ItemSource 是一个包含您的模型产品键和值的字典。

添加:

 Dictionary<string, string> Products{ get; set; }

到您的模型并用它的产品填充每个模型。

【讨论】:

  • 如何从数据表中检索到 Products 的值?如何使用 LINQ 查询?
【解决方案2】:

您可以从 dataTable 创建自定义 DataGrid:

1.Xaml:

   <DataGrid x:Name="DataGrid" Grid.Column="0" Grid.Row="0"
                      ItemsSource="{Binding GenericDataView}"
                      attachedBehaviors:DataGridColumnsBehavior.BindableColumns="{Binding GridColumns}"
                      AutoGenerateColumns="False"
                      EnableRowVirtualization="False"
                      />

2.填写你的数据表 3.创建GridColumnsCollection和DataView:

 GridColumns = new ObservableCollection<DataGridColumn>();
  public DataView GenericDataView { get; private set; }

4.浏览你的dataTable行并创建你的自定义列,这里是动态创建的ComboBoxColumn示例:

        ItemsProducts = new ObservableCollection<productModel>();
                                        ItemsProducts = YourProducts           
                                        DataGridComboBoxColumn comboBoxColumn = new DataGridComboBoxColumn();
                                        comboBoxColumn.Header = row["displayname"].ToString();
                                        comboBoxColumn.SelectedValuePath = "Key";
                                        comboBoxColumn.DisplayMemberPath = "Value";
                                        Binding binding = new Binding();
                                        binding.Path = new PropertyPath(row["name"].ToString());
                                        comboBoxColumn.SelectedValueBinding = binding;
                                        Binding itemsSourceBinding = new Binding();
                                        itemsSourceBinding.Source = ItemsProducts;
                                        BindingOperations.SetBinding(comboBoxColumn, DataGridComboBoxColumn.ItemsSourceProperty, itemsSourceBinding);

                                        GridColumns.Add(comboBoxColumn);
                                    }
                                    ColumnsList.Add(row["name"].ToString()); //add the real column name

5.循环所有行并填充数据后:

    RaisePropertyChanged("GridColumns");
    GenericDataView = new DataView(dt);
    RaisePropertyChanged("GenericDataView");

【讨论】:

  • 感谢您的回复。但我不知道如何从 xml 文件中获取“产品值”以将其分配给以下语句。 ItemsProducts = YourProducts
  • 使用 Linq:var WGTS = XDocument.Load(XmlFilePath).Descendants("WGT"); foreach (WGTS 中的 XElement xElement) { foreach (xElement.Elements("Product") 中的 var p) { } }
【解决方案3】:

我觉得你可以试试这个方法,在dt中你可以得到产品值

房产

 private DataView dt;
        public DataView DT
        {

            get
            {
                return dt;
            }
            set
            {
                dt = value;
                NotifyPropertyChanged("DT");
            }
        }

视图模型:

XmlDocument doc = new XmlDocument();
            doc.Load("d:\\file.txt");
            XmlReader xmlFile = new XmlNodeReader(doc);

           DataSet ds = new DataSet();
           ds.ReadXml(xmlFile);
           DT = ds.Tables[1].AsDataView();

XAML:

<ComboBox x:Name="cmbBox" ItemsSource="{Binding DT}" Width="100" Height="100" DisplayMemberPath="Product_Text" SelectedValuePath="WGT_Id"></ComboBox>

【讨论】:

  • 贾斯汀 CI,感谢您的回复。我之前尝试过同样的方法,问题是 TName 和 Fname 在 datagrid 中显示良好,但产品值没有填充到 datagrid 单元格模板内的组合框中。
  • 有人可以验证这个说法吗?这会拉动所有产品价值吗? List ids = dt.AsEnumerable().Select(r => r.Field("Product")).ToList();
  • @ganeshshsf 您正在使用 ds.Tables[0] 将其更改为 ds.Tables[1]
  • 如果我将 ds.Tables[0] 更改为 ds.Tables[1] ,其他 2 个字段不会出现,即 datagrid 中的 TName 和 Fname。
猜你喜欢
  • 2020-09-22
  • 2017-12-16
  • 2021-02-20
  • 2020-01-18
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
相关资源
最近更新 更多