如何根据 XML 中的属性设置绑定到 XML 的 ComboBox 的 SelectedIndex 属性?
我有一个如下定义的 WPF 组合框:
<ComboBox Grid.Column="1" x:Name="cUrls" SelectedIndex="1" ItemsSource=" {Binding XPath=//data/endpoints/endpoint}" Margin="5" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
窗口绑定到 XmlDocument,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<data>
<endpoints>
<endpoint name="test">test url</endpoint>
<endpoint default="true" name="production">production url</endpoint>
</endpoints>
<requests>
<request >
...
</request>
<request >
...
</request>
</requests>
</data>
绑定工作正常,组合框显示项目“测试”和“生产”,我能够从 SelectedValue 中提取正确的 URL财产。
我希望能够将 ComboBox 上的 SelectedIndex
属性设置为具有 default=true
的
节点的索引属性。
我可以在 ComboBox 上执行 SelectedIndex="{Binding XPath=}"
操作吗?如果是,那表情会是什么样子?如果没有,我该怎么办?
谢谢!
I have a WPF Combobox defined as such:
<ComboBox Grid.Column="1" x:Name="cUrls" SelectedIndex="1" ItemsSource=" {Binding XPath=//data/endpoints/endpoint}" Margin="5" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The window is bound to an XmlDocument like this:
<?xml version="1.0" encoding="utf-8" ?>
<data>
<endpoints>
<endpoint name="test">test url</endpoint>
<endpoint default="true" name="production">production url</endpoint>
</endpoints>
<requests>
<request >
...
</request>
<request >
...
</request>
</requests>
</data>
The binding works fine and the combo box shows the items "test" and "production" and I am able to pull the right URL out of the SelectedValue property.
I would like to be able to set the SelectedIndex
property on the ComboBox to the index of the <endpoint>
node that has default=true
attribute.
Can I do SelectedIndex="{Binding XPath=}"
on the ComboBox? If yes, what would that expression look like? If not, what should I do?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
Try