【问题标题】:How to bind data from xml tag to combo box in xamdatagrid or datagrid in wpf?如何将 xml 标签中的数据绑定到 wpf 中 xamdatagrid 或 datagrid 中的组合框?
【发布时间】:2017-01-29 04:10:30
【问题描述】:

xml 文件。

<Products>
   <ProductTemplate>
       <ProductID> 1</ProductID>
       <ProductLot>11</ProductLot>
       <Product>product1</product>
       <Product>product1</product>
<ProductTemplate>
<Products>
 .....
  ....

我能够在 xamdatagrid 中获取“productID”和“productLot”。现在我必须将“产品”作为组合框添加到 xamdatagrid 中。下面是 xaml 文件中的代码。

                    <igDP:UnboundField Name="Product" Label="Product Name">
                        <igDP:UnboundField.Settings>
                            <igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}" AllowEdit="True">
                                <igDP:FieldSettings.EditorStyle>
                                    <Style TargetType="{x:Type igEditors:XamComboEditor}">
                                        <EventSetter Event="Loaded" Handler="Combo_Loaded"/>
                                        <Setter Property="ItemsSource" Value="{Binding Items}" />

                                    </Style>
                                </igDP:FieldSettings.EditorStyle>
                            </igDP:FieldSettings>
                        </igDP:UnboundField.Settings>
                    </igDP:UnboundField>
                </igDP:FieldLayout.Fields>
            </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>
    </igDP:XamDataGrid>
</Grid>

现在的挑战是组合框中没有填充“产品”值。

下面是display()函数viewmodel类

public void display(XamDataGrid dataGridView1)
        {


            XmlReader xmlFile;
            xmlFile = XmlReader.Create("C:/Wafers/WaferGen.xml", new XmlReaderSettings());
            DataSet ds = new DataSet();
            ds.ReadXml(xmlFile);
            dataGridView1.DataContext = ds.Tables[0]; 
}

我想知道如何从 xml 文件中检索“产品”中的值,然后如何绑定到 xamdatagrid 内的组合框?

【问题讨论】:

    标签: c# wpf xaml combobox xamdatagrid


    【解决方案1】:

    XML 格式不正确。下面将帮助您从 XML 中获取产品列表,然后您可以将其绑定到您想要的控件,在您的情况下为 combobox

    string productXML = @"<Products>
                            <ProductTemplate>
                              <ProductID> 1</ProductID>
                              <ProductLot>11</ProductLot>
                              <Product>product1</Product>
                              <Product>product1</Product>
                            </ProductTemplate>
                         </Products>";
    
    XDocument xdoc = XDocument.Parse(productXML);
    //This will give you the list of Products which you can bind with your control
    var listOfProducts = xdoc.Descendants("Products").Elements("ProductTemplate").Descendants().Where(x => x.Name == "Product").Select(x => x.Value).ToList();
    

    【讨论】:

    • 感谢您的回复和您的宝贵时间 Richa Garg。解决方案在我的代码中不起作用。我正在从“product.xml”文件中获取输入。所以我尝试使用 XDocument xdoc = XDocument.Load("C:/Products/Product.xml") - 它不起作用。我也试过你的代码 sn-p 。没有运气。数据未填充到 xamdatagrid 的组合框中。我不确定我的约束性声明是否正确。
    • 你可以尝试从数据网格中绑定一个组合框,只是为了检查绑定是否有问题。由于我的代码在我的机器上运行
    • 组合框正在 xamdatagrid 中填充,但列表为空。想不通..
    • Richa Garg ,您的解决方案现在运行良好。我能够在我的代码中弄清楚。感谢您宝贵的时间和帮助。
    猜你喜欢
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2014-10-23
    相关资源
    最近更新 更多