1

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!

H.B.
  • 166,899
  • 29
  • 327
  • 400
Matthew
  • 2,210
  • 1
  • 19
  • 30

1 Answers1

1

Try

      <ComboBox x:Name="cUrls" 
                SelectedItem="{Binding XPath=/data/endpoints/endpoint[@default\=\'true\']}"
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Nope. Neither does adding /position() at the end of the XPath – Matthew Apr 08 '11 at 19:42
  • That doesn't make any difference. – Matthew Apr 08 '11 at 19:50
  • @Matthew Try this edited snippet. I just tested something like this. – Bala R Apr 08 '11 at 20:18
  • Sorry to undo your correct answer but there is a problem. It seems that whenever I attempt to access the `SelectedItem` property on the ComboBox it is _always_ the `@default=true` element even after I change the combobox. The value of the SelectedItem property doesn't change when I change the combobox at runtime in the UI. Any ideas? – Matthew Apr 08 '11 at 20:58