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!

1

There are 1 best solutions below

4
On

Try

      <ComboBox x:Name="cUrls" 
                SelectedItem="{Binding XPath=/data/endpoints/endpoint[@default\=\'true\']}"