none
Load xml data into 1 ComboBox and 1 TextBox RRS feed

  • Question

  • Terms.xml ----

    <?xml version="1.0" encoding="utf-8" ?>
    <Terms>
    <Item Name="Muddy" Surname="Waters" />
    <Item Name="Roy" Surname="Buchanan" />
    </Terms>

    -----------------

    xaml ---------

    <Window.Resources>
    <XmlDataProvider
    x:Key="XmlData"
    Source="d:\Terms.xml"
    XPath="Terms" />
    </Window.Resources>
    <Grid>
    <ComboBox
    x:Name="ComboBox1"
    IsEditable="True"
    ItemsSource="{Binding Source={StaticResource XmlData}, XPath=./Item}"
    DisplayMemberPath="@Name"
    SelectionChanged="ComboBox1_SelectionChanged"
    Width="240"
    Grid.Column="0"
    Grid.Row="0"
    Height="25"
    HorizontalAlignment="Left"
    VerticalAlignment="Stretch"
    Margin="20,50,0,0"/>
    <TextBox
    x:Name="TextBox1"
    Width="240"
    Height="25"
    Grid.Column="0"
    Grid.Row="0"
    HorizontalAlignment="Left"
    VerticalAlignment="Bottom"
    Margin="20,70,0,80" />
    </Grid>

    -------------------------------------------------

    I'd like to make TextBox1 display the Surname of the corresponding Name once selected in Combobox1.

    Any suggestions?

     


    • Edited by John-33 Saturday, June 15, 2019 10:22 AM
    Saturday, June 15, 2019 10:22 AM

Answers

All replies

  • Hi,
    use element binding to get the Surname attribute value:

        <ComboBox
          x:Name="ComboBox1"
          IsEditable="True"
          ItemsSource="{Binding Source={StaticResource XmlData}, XPath=./Item}"
          DisplayMemberPath="@Name"
          Width="240"
          Grid.Column="0"
          Grid.Row="0"
          Height="25"
          HorizontalAlignment="Left"
          VerticalAlignment="Stretch"
          Margin="20,50,0,0"/>
        <TextBox
          x:Name="TextBox1"
          DataContext="{Binding ElementName=ComboBox1, Path=SelectedItem}"
          Text="{Binding XPath=@Surname}"
          Width="240"
          Height="25"
          Grid.Column="0"
          Grid.Row="0"
          HorizontalAlignment="Left"
          VerticalAlignment="Bottom"
          Margin="20,70,0,80" />
      </Grid>


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Saturday, June 15, 2019 6:30 PM
  • Hi Peter,

    brilliant!

    Besten Dank ;)

    • Marked as answer by John-33 Sunday, June 16, 2019 4:25 AM
    Sunday, June 16, 2019 4:25 AM