wewa
wewa

Reputation: 1678

Bind selected XML Content to Textbox

An XML file should be opened and the elements should be displayed in a ListBox where single elements can be selected. The selected elements should be displayed in a TextBox where they can be changed.

I managed to open and display the XML file in the ListBox. But how can I manage to display the selected XML content in the 2 TextBoxes?

Here is the content of the XML file.

<Inventory xmlns="">
    <Books>
        <Book ISBN="0-7356-0562-9" Stock="in" Number="9">
            <Title>XML in Action</Title>
            <Summary>XML Web Technology</Summary>
        </Book>
        <Book ISBN="0-7356-1370-2" Stock="in" Number="8">
            <Title>Programming Microsoft Windows With C#</Title>
            <Summary>C# Programming using the .NET Framework</Summary>
        </Book>
        <Book ISBN="0-7356-1288-9" Stock="out" Number="7">
            <Title>Inside C#</Title>
            <Summary>C# Language Programming</Summary>
        </Book>
        <Book ISBN="0-7356-1377-X" Stock="in" Number="5">
            <Title>Introducing Microsoft .NET</Title>
            <Summary>Overview of .NET Technology</Summary>
        </Book>
        <Book ISBN="0-7356-1448-2" Stock="out" Number="4">
            <Title>Microsoft C# Language Specifications</Title>
            <Summary>The C# language definition</Summary>
        </Book>
    </Books>
</Inventory>

And here is the content of MainWindow.xaml.

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="XML_View_Edit.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Width="500"
    Height="200">
    <Window.Resources>
        <XmlDataProvider x:Key="InventoryData"
                         XPath="Inventory/Books"
                         Source="Data.xml"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition
                Height="100" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <ListBox
            Grid.Row="0"
            Name="listBox1" >
            <ListBox.ItemsSource>
                <Binding Source="{StaticResource InventoryData}" XPath="Book"/>
            </ListBox.ItemsSource>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text = "{Binding XPath=Title}" />
                        <TextBlock Text= " - " />
                        <TextBlock Text = "{Binding XPath=Summary}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Grid
            Grid.Row="1"
             DataContext="listBox1.SelectedItem">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition
                    Width="70" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Label
                Content="Title"
                Grid.Row="0"
                Grid.Column="0"
                Name="label_title" />
            <Label
                Content="Summary"
                Grid.Row="1"
                Grid.Column="0"
                Name="label_summary" />
            <TextBox
                Grid.Column="1"
                Name="textbox_title"
                Grid.Row="0" />
            <TextBox
                Grid.Column="1"
                Name="textbox_summary"
                Grid.Row="1" />
        </Grid>
    </Grid>
</Window>

The Title and Summary of the selected Book should be displayed in the 2 TextBoxes, where the user can change each value.

Upvotes: 1

Views: 2205

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273179

Roughly:

  • place the 2 TextBoxes on a Container C1
  • Bind C1.DataContext to ListBox1.SelectedItem

And I'm not sure what results XPath="*" in the ListBox1 binding wil give. You probably want something like:

<Binding Source="{StaticResource InventoryData}" XPath="@ISBN"/>

Edit

You need to actually bind the TextBox elements:

      <TextBox
            Grid.Column="1"
            Name="textbox_title"
            Text = "{Binding XPath=Title}"   <!-- this -->
            Grid.Row="0" />
        <TextBox
            Grid.Column="1"
            Name="textbox_summary"
            Text = "{Binding XPath=Summary}"   <!-- and this -->
            Grid.Row="1" />

Give that a try.

Edit 2:

And let's fix the binding of the inner Grid:

  DataContext="{Binding ElementName=listBox1, Path=SelectedItem}"

Upvotes: 1

Tilak
Tilak

Reputation: 30688

Try the following:

<ListBox
    Grid.Row="0"
    Name="listBox1" >
    <ListBox.ItemsSource>
        <Binding Source="{StaticResource InventoryData}" XPath="*"/>
</ListBox.ItemsSource>
 <ListBox.ItemTemplate>
  <DataTemplate>
    <StackPanel Orientation="Horizontal">
      <TextBlock Text = "{Binding XPath=Title}">
      <TextBlock Text = "{Binding XPath=Summary}">
    </StackPanel>
  </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

Upvotes: 0

Related Questions