博客园  :: 首页  :: 订阅 订阅  :: 管理

WPF数据绑定到各种数据源系列(XML数据源)

Posted on 2010-08-11 18:56  卞红军  阅读(1794)  评论(1编辑  收藏  举报

转自:http://blog.sina.com.cn/s/blog_4acbea900100hirl.html

 

Xml数据源,主要就是分为嵌在页面内部的和外部的xml两种格式,其实严格的说来就是一种。因为二者的处理方式相当于一种。
(1) 内嵌XML数据源
首先我们将XML写到页面资源处。

然后看我们的listView的代码,我们看到了因为用到了XML数据源,因此就用XPath这个属性。还有就是如果出现的关键字也是里面出现的,那么就需要加上一个@字符。效果图与上面的是完全一致的。

(2) 外部XML数据源
在用到外部数据源的时候会更简单些,我们将上面的xml数据写到一个XML文档里,然后看下面代码:

<Window.Resources >
        <cnv:MyConvert x:Key ="MyConvert"></cnv:MyConvert>
        <XmlDataProvider x:Key="Mybook" Source="/BOOK.xml"></XmlDataProvider>
</Window.Resources>

这里就是引用外部文件的代码了,当然,Source可以是文件的路径也可以是网址。下面的都是相同的,其余的就没什么了。

 

下面本文代码:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:me="clr-namespace:WpfApplication8"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <XmlDataProvider x:Key="Book" Source="Books.xml">
        </XmlDataProvider>
        <me:PageConvertToBrush x:Key="pageConvertToBrush"></me:PageConvertToBrush>
    </Window.Resources>
    
    <Grid DataContext="{Binding ElementName=listView1, Path=SelectedItem}" Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <ListView x:Name="listView1" ItemsSource="{Binding Source={StaticResource ResourceKey=Book}, XPath=/Book/mybook}"
                  SelectionMode="Single">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="书籍编号" DisplayMemberBinding="{Binding XPath=bookID}"></GridViewColumn>
                    <GridViewColumn Header="书籍名称" DisplayMemberBinding="{Binding XPath=@Name}"></GridViewColumn>
                    <GridViewColumn Header="书籍ISBN" DisplayMemberBinding="{Binding XPath=bookISBN}"></GridViewColumn>
                    <GridViewColumn Header="书籍页数">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding XPath=bookPage}"
                                           Foreground="{Binding XPath=bookPage, Converter={StaticResource ResourceKey=pageConvertToBrush}}"></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <Grid Grid.Row="1" MinWidth="200" HorizontalAlignment="Center" Margin="5">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Text="书籍编号: "/>
            <TextBox Grid.Column="1" Text="{Binding XPath=bookID}"></TextBox>
            <TextBlock Grid.Row="1" Text="书籍名称: "/>
            <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding XPath=@Name}"></TextBox>
            <TextBlock Grid.Row="2" Text="书籍ISBN: "/>
            <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding XPath=bookISBN}"></TextBox>
            <TextBlock Grid.Row="3" Text="书籍页数: "/>
            <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding XPath=bookPage}"></TextBox>
        </Grid>
    </Grid>
</Window>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Globalization;

namespace WpfApplication8
{
    [ValueConversion(typeof(Brush), typeof(string))]
    class PageConvertToBrush : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo colture)
        {
            int pageCount;
            int.TryParse((string)value, out pageCount);
            if (pageCount > 500)
                return Brushes.Red;
            else
                return Brushes.Black;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("不能根据背景色判断书籍的页数!");
        }
    }
}

 

<?xml version="1.0" encoding="utf-8" ?>
<Book xmlns="">
  <mybook Name="深入解析WPF编程">
    <bookID>1</bookID>
    <bookISBN>978-7-121-05859-2</bookISBN>
    <bookPage>312</bookPage>
  </mybook>
  <mybook Name="WPF高级编程">
    <bookID>2</bookID>
    <bookISBN>978-7-121-05859-2</bookISBN>
    <bookPage>1000</bookPage>
  </mybook>
  <mybook Name="silverlight入门">
    <bookID>3</bookID>
    <bookISBN>978-7-121-05859-2</bookISBN>
    <bookPage>63</bookPage>
  </mybook>
</Book>

 

程序运行效果: