本文介绍了从组合框项中获取XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用WPF,我可以使用WPF使用以下代码填充来自xml文件的元素的组合框

 < ComboBox Grid.Column =1Height =21Horizo​​ntalAlignment =LeftMargin =0,32,0,0Name =QueryChooserVerticalAlignment =TopWidth =189ItemsSource ={Binding}SelectionChanged =QueryChooser_SelectionChanged/> 

我的xaml.cs

  private void Window_Loaded(object sender,RoutedEventArgs e)
{
Queryslistload();
}

private void Queryslistload()
{
var xElem = XElement.Load(@Querys.xml);

var querys =来自xElem.Descendants(QueryLay)中的查询
orderby query.Element(QueryName)。value
select query.Element(QueryName) 。值;
QueryChooser.ItemsSource = querys;

}

这是我的xml文件本身

 <?xml version =1.0encoding =utf-8standalone =yes?> 
< Querys>
< QueryLay>
< QueryID>
1
< / QueryID>
< QueryName>检查Logspace< / QueryName>
< Query> dbcc sqlperf(logspace)< / Query>
< / QueryLay>
< QueryLay>
< QueryID>
2
< / QueryID>
< QueryName> Check Spaceused< / QueryName>
< Query> sp_spaceused< / Query>
< / QueryLay>

< / Querys>

所以现在如果用户从组合框中选择检查空间,我希望查询元素显示在文本框



如何实现?



UPDATED

  public class Query 
{
public int Id {get;组; }
public string Name {get;组; }
public string Value {get;组;
}
private void QueryChooser_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
var xElem = XElement.Load(@Querys.xml);
var querys = xElem.Descendants(QueryLay)。选择(e =>
new Query {
Id = Convert.ToInt32(e.Element(QueryID)。 ,
Name = e.Element(QueryName)Value,
Value = e.Element(Query)。value
})。OrderBy(q => q.Name )
select query.Element(QueryName)。

listBox1.ItemsSource = querys;
}


解决方案

而不是绑定 ComboBox 直接从您的查询返回的 XElement ,您应该创建表示QueryName / Query组合的自己的类型,然后定义一个LINQ-to-XML查询,它将元素投射到这种类型的序列。



然后可以绑定 ComboBox SelectedItem 到数据上下文中的一个属性。



例如:



查询类型

  public class Query 
{
public int Id {get;组; }
public string Name {get;组; }
public string Value {get;组;
}

数据上下文

  var xElem = XElement.Load(@Querys.xml); 

this.Queries = xElem.Descendants(QueryLay)。选择(e =>
new Query
{
Id = Convert.ToInt32(e。 Element(QueryID)。
Name = e.Element(QueryName)。value,
Value = e.Element(Query)。value
})。 OrderBy(q => q.Name);

public查询SelectedQuery {get;组; }

查看




SelectedItem ={Binding SelectedQuery}
DisplayMemberPath =Name
.. 。/>

this.SelectedQuery.Value 在您的数据然后,上下文将为您提供所选的查询值。


I am trying to get an XML element in a textbox from the users choosen combox element also from the same XML file.

I am using WPF, i am able to populate the combobox with the elements from the xml file using the following code

 <ComboBox  Grid.Column="1" Height="21"  HorizontalAlignment="Left" Margin="0,32,0,0" Name="QueryChooser" VerticalAlignment="Top" Width="189" ItemsSource="{Binding}" SelectionChanged="QueryChooser_SelectionChanged" />

My xaml.cs

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Queryslistload();
        }

        private void Queryslistload()
        {
            var xElem = XElement.Load(@"Querys.xml");

            var querys = from query in xElem.Descendants("QueryLay")
                         orderby query.Element("QueryName").Value
                         select query.Element("QueryName").Value;
            QueryChooser.ItemsSource = querys;

        }

this is my xml file itself

<?xml version="1.0" encoding="utf-8"  standalone="yes" ?>
<Querys>
    <QueryLay>
    <QueryID>
        1
    </QueryID>
    <QueryName>Check Logspace</QueryName>
    <Query>dbcc sqlperf(logspace)</Query>
    </QueryLay>
    <QueryLay>
    <QueryID>
        2
    </QueryID>
    <QueryName>Check Spaceused</QueryName>
    <Query>sp_spaceused</Query>
    </QueryLay>

    </Querys>

so now if the user selects the check logspace from combobox i want the query element to be displayed in the textbox

how do i achieve this?

UPDATED

public class Query
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Value { get; set; }
        }
        private void QueryChooser_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var xElem = XElement.Load(@"Querys.xml");
            var querys =  xElem.Descendants("QueryLay").Select( e =>
                new Query{
                         Id = Convert.ToInt32(e.Element("QueryID").Value),
       Name = e.Element("QueryName").Value,
       Value = e.Element("Query").Value
                         }).OrderBy(q=>q.Name)
                         select query.Element("QueryName").Value ;

            listBox1.ItemsSource = querys;
        }
解决方案

Rather than bind the ComboBox directly to the XElement's returned from your query, you should create your own type which represents a QueryName/Query combination, and then define a LINQ-to-XML query which projects the elements to a sequence of this type.

You can then bind the ComboBox SelectedItem to a property on your data context.

E.g:

Query Type

public class Query
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string Value { get; set; }
}

Data Context

var xElem = XElement.Load(@"Querys.xml");

this.Queries = xElem.Descendants("QueryLay").Select(e =>
     new Query
     {
       Id = Convert.ToInt32(e.Element("QueryID").Value),
       Name = e.Element("QueryName").Value,
       Value = e.Element("Query").Value
     }).OrderBy(q => q.Name);

public Query SelectedQuery { get; set; }

View

<ComboBox ItemsSource="{Binding Queries}"
    SelectedItem="{Binding SelectedQuery}"
    DisplayMemberPath="Name"
    ... />

this.SelectedQuery.Value in your data context will then give you the selected query value.

这篇关于从组合框项中获取XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:52