How can I set the SelectedIndex property of a ComboBox that is bound to XML based on an attribute in that xml?
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 SelectedI开发者_开发问答ndex="{Binding XPath=}"
on the ComboBox? If yes, what would that expression look like? If not, what should I do?
Thanks!
Try
<ComboBox x:Name="cUrls"
SelectedItem="{Binding XPath=/data/endpoints/endpoint[@default\=\'true\']}"
精彩评论