在Silverlight中将xml数据重新引用到组合框 [英] re-referencing xml data to combobox in silverlight
问题描述
大家好,
我为此感到难过.
在我的silvelrght应用程序上,我有2个组合框,应该从xml数据表填充.因此,让我们说一个数据样本:
Hi All,
I''m stumped with this one.
on my silvelrght app, I have 2 comboboxes, that are supposed to populate from an xml data sheet. So lets say here''s a data sample:
<model name="Accord 4DR">
<code name="CP2E6CE" />
<code name="CP2F6CE" />
<code name="CP2F7CJ" />
<code name="CP2F8CJN" />
<code name="CP2F8CKN" />
<code name="CP3F8CJN" />
<code name="CP3F8CKN" />
</model>
<model name="Civic 2DR">
<code name="FG3A4CE" />
<code name="FG3B4CE" />
<code name="FG3A5CJ" />
<code name="FG3B5CJ" />
<code name="FG3B9CKN" />
<code name="FG4A5CK" />
</model>
对于第一个组合框,其填充如下:
For the first combobox, it populates likes this:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
XDocument xml = XDocument.Load("modelcodes.xml");
foreach (XElement element in xml.Descendants("model"))
{
CarTypeCB.Items.Add(element.FirstAttribute.Value);
}
}
现在,第二个组合框需要填充我在第一个组合框的SelectionChanged上所做的任何选择的后代.我无法弄清楚什么是正确的语法,基本上使用SelectedItem.Value告诉它从哪里获取后代.我想做类似的事情,所以我说类似:
Now, the second combobox needs to populate with the descendants of whatever selection I make on SelectionChanged of the first combobox. I cant figure out what the right syntax is for basically using the SelectedItem.Value to tell it where to get the descendants from. I want to do something similar so, id say something like:
private void CarTypeCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ModelCodeCB.IsEnabled = true;
XDocument xml = XDocument.Load("modelcodes.xml");
var query = from selection in xml.Descendants("model")
where (string)selection == (ModelCodeCB.SelectedValue)
from codes in selection.Elements("code")
select codes;
foreach (XElement codes in query.Descendants("code"))
{
ModelCodeCB.Items.Add(codes.FirstAttribute.Value);
}
}
但它似乎没有响应,也没有填充组合框.正确的语法是什么?
but it doesnt seem to respond and it doesn''t populate the combobox. What would the proper syntax be?
推荐答案
private void CarTypeCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ModelCodeCB.IsEnabled = true;
XDocument xml = XDocument.Load("modelcodes.xml");
var query = from selection in xml.Descendants("model")
where (string)selection == (ModelCodeCB.SelectedValue)
from codes in selection.Elements("code")
select codes;
foreach (XElement codes in query.Descendants("code"))
{
ModelCodeCB.Items.Add(codes.FirstAttribute.Value);
}
}
尝试替换为:
Try to replace for this:
private void CarTypeCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ModelCodeCB.IsEnabled = true;
XDocument xml = XDocument.Load("modelcodes.xml");
var query = from selection in xml.Descendants("model")
where (string)selection == (ModelCodeCB.SelectedValue.ToString())
from codes in selection.Elements("code")
select codes;
foreach (XElement codes in query)
{
ModelCodeCB.Items.Add(codes.FirstAttribute.Value);
}
}
希望对您有所帮助.
Hope it helps.
这篇关于在Silverlight中将xml数据重新引用到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!