应打开XML文件,并且元素应显示在ListBox
中,其中可以选择单个元素。所选元素应显示在可以更改的TextBox
中。
我设法在ListBox
中打开并显示XML文件。但是,我如何设法在2 TextBox
es?
以下是XML文件的内容。
<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>
这是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>
所选书籍的标题和摘要应显示在2 TextBox
es中,用户可在其中更改每个值。
答案 0 :(得分:1)
粗略地说:
TextBox
es放在容器C1
C1.DataContext
绑定到ListBox1.SelectedItem
我不确定XPath="*"
约束中的ListBox1
结果会给出什么结果。你可能想要这样的东西:
<Binding Source="{StaticResource InventoryData}" XPath="@ISBN"/>
您需要实际绑定TextBox
元素:
<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" />
试一试。
让我们修复内部网格的绑定:
DataContext="{Binding ElementName=listBox1, Path=SelectedItem}"
答案 1 :(得分:0)
尝试以下方法:
<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>