WPF绑定XML ListBox显示
2009-10-13 12:34
169 查看
Window1.xaml 前台代码
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPFDataBindingXML" Height="372" Width="462">
<Window.Resources>
<XmlDataProvider x:Key="guestSource" Source="guest.xml" XPath="/Guests/*"/>
<DataTemplate x:Key="showTitle">
<TextBlock Text="{Binding XPath=Title}"/>
</DataTemplate>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontFamily" Value="Trebuchet MS"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding Source={StaticResource guestSource}}"
ItemTemplate="{StaticResource showTitle}" IsSynchronizedWithCurrentItem="True">
</ListBox>
<StackPanel Grid.Row="0" Grid.Column="1" Margin="5,5,5,5">
<TextBlock>
Title:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Title}"/>
</TextBlock>
<TextBlock>
Coment:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Coment}"/>
</TextBlock>
<TextBlock>
Reply:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Reply}"/>
</TextBlock>
</StackPanel>
</Grid>
</Window>
******************************************************
guest.xml 数据结构
<?xml version="1.0" encoding="utf-8" ?>
<Guests>
<xs:schema id="Guests" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Guests" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Guest">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string" minOccurs="0" />
<xs:element name="Coment" type="xs:string" minOccurs="0" />
<xs:element name="Reply" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Guest>
<Title>sohu</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>why?</Title>
<Coment>because ?</Coment>
<Reply>嘿嘿?</Reply>
</Guest>
<Guest>
<Title>fet</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>goog</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>heal</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
</Guests>
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPFDataBindingXML" Height="372" Width="462">
<Window.Resources>
<XmlDataProvider x:Key="guestSource" Source="guest.xml" XPath="/Guests/*"/>
<DataTemplate x:Key="showTitle">
<TextBlock Text="{Binding XPath=Title}"/>
</DataTemplate>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontFamily" Value="Trebuchet MS"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding Source={StaticResource guestSource}}"
ItemTemplate="{StaticResource showTitle}" IsSynchronizedWithCurrentItem="True">
</ListBox>
<StackPanel Grid.Row="0" Grid.Column="1" Margin="5,5,5,5">
<TextBlock>
Title:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Title}"/>
</TextBlock>
<TextBlock>
Coment:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Coment}"/>
</TextBlock>
<TextBlock>
Reply:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Reply}"/>
</TextBlock>
</StackPanel>
</Grid>
</Window>
******************************************************
guest.xml 数据结构
<?xml version="1.0" encoding="utf-8" ?>
<Guests>
<xs:schema id="Guests" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Guests" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Guest">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string" minOccurs="0" />
<xs:element name="Coment" type="xs:string" minOccurs="0" />
<xs:element name="Reply" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Guest>
<Title>sohu</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>why?</Title>
<Coment>because ?</Coment>
<Reply>嘿嘿?</Reply>
</Guest>
<Guest>
<Title>fet</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>goog</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>heal</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
</Guests>
相关文章推荐
- WPF绑定XML ListBox显示
- WPF中ListView绑定数据源后居中显示
- 在WPF中绑定类的属性至Label控件实现自动更新显示
- 使用数据绑定和 WPF 自定义数据显示
- [原创]WPF中ListView绑定数据源后居中显示
- WPF中绑定时显示指定绑定源的3种方法
- WPF——TargetNullValue(如何在绑定空值显示默认字符)
- wpf下textbox绑定list<string>显示
- WPF -- 绑定显示多个属性
- WPF中在listview进行数据绑定,根据绑定数据调整一列的显示
- 使用数据绑定和 WPF 自定义数据显示
- WPF4数据绑定应用之“创建具有多种显示效果的字串”
- WPF一个绑定格式化显示的写法
- C# wpf image绑定viewModel没有显示图片
- C# wpf image绑定viewModel没有显示图片
- WPF 绑定实例之 LISTBOX显示 图片列表
- WPF中DataGrid控件的数据绑定与显示数组
- WPF{ComboBox绑定类对象, 下拉列显示的值,与取到的值}
- 基于WPF数据绑定的鼠标随动显示光标位置的方法
- wpf数据绑定——radiobutton绑定枚举值并显示枚举值