参考网上查到的相关帖子总感觉不够全面,尤其是使用纯粹的Mvvm架构的。
本人综合了各种例子,用 Mvvm Light 模板建立了一个 Binding to XML例子的项目贴出来分享
里面使用了4个方法,第一个方法没有使用Mvvm,其他三个方法使用了Mvvm,总的感觉最后一个方法好一点。
先看xaml:
<Window x:Class="MvvmLight1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ignore="http://www.ignore.com" mc:Ignorable="d ignore" Height="600" Width="800" Title="MVVM Light Application" DataContext="{Binding Main,Source={StaticResource Locator}}"> <Grid x:Name="LayoutRoot"> <Grid.RowDefinitions> <RowDefinition Height="25"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <TextBlock FontSize="16" FontWeight="Bold" Foreground="Purple" Text="{Binding WelcomeTitle}" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap" /> <StackPanel Grid.Row="1"> <TextBlock Text="没有使用Mvvm的两个列子" FontWeight="Bold" /> <Grid > <Grid.Resources> <XmlDataProvider x:Key="xml_database" Source="/model/database.xml" /> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <DataGrid DataContext="{StaticResource xml_database}" ItemsSource="{Binding XPath=/Database/Table1/Tiger}" AutoGenerateColumns="False" Margin="10,10,10"> <DataGrid.Columns> <DataGridTextColumn Header="No" Binding="{Binding XPath=@No}" /> <DataGridTextColumn Header="Sex" Binding="{Binding XPath=@Sex}" /> <DataGridTextColumn Header="Weight" Binding="{Binding XPath=@Weight}"/> </DataGrid.Columns> </DataGrid> <DataGrid Grid.Row="1" DataContext="{StaticResource xml_database}" ItemsSource="{Binding XPath=/Database/Table2/Dog}" AutoGenerateColumns="False" Margin="10,10"> <DataGrid.Columns> <DataGridTextColumn Header="No" Binding="{Binding XPath=@Nickname}" /> <DataGridTextColumn Header="Sex" Binding="{Binding XPath=@Sex}" /> <DataGridTextColumn Header="Variety" Binding="{Binding XPath=@Variety}" /> </DataGrid.Columns> </DataGrid> </Grid> <TextBlock Text="使用Mvvm的列子" FontWeight="Bold"></TextBlock> <TextBlock Text="使用Mvvm的列子 方法一" FontWeight="Bold"></TextBlock> <DataGrid DataContext="{Binding TigerXmlProvider}" ItemsSource="{Binding}" Background="AliceBlue" AutoGenerateColumns="False" > <DataGrid.Columns> <DataGridTextColumn Header="No" Binding="{Binding XPath=@No}" /> <DataGridTextColumn Header="Sex" Binding="{Binding XPath=@Sex}" /> <DataGridTextColumn Header="Weight" Binding="{Binding XPath=@Weight}"/> </DataGrid.Columns> </DataGrid> <DataGrid DataContext="{Binding DogXmlProvider}" ItemsSource="{Binding}" Background="AliceBlue" AutoGenerateColumns="False" > <DataGrid.Columns> <DataGridTextColumn Header="No" Binding="{Binding XPath=@Nickname}" /> <DataGridTextColumn Header="Sex" Binding="{Binding XPath=@Sex}" /> <DataGridTextColumn Header="Variety" Binding="{Binding XPath=@Variety}" /> </DataGrid.Columns> </DataGrid> <TextBlock Text="使用Mvvm的列子 方法二" FontWeight="Bold"></TextBlock> <DataGrid DataContext="{Binding Database}" ItemsSource="{Binding XPath=/Database/Table1/Tiger}" Background="Bisque" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="No" Binding="{Binding XPath=@No}" /> <DataGridTextColumn Header="Sex" Binding="{Binding XPath=@Sex}" /> <DataGridTextColumn Header="Weight" Binding="{Binding XPath=@Weight}"/> </DataGrid.Columns> </DataGrid> <DataGrid DataContext="{Binding Database}" ItemsSource="{Binding XPath=/Database/Table2/Dog}" Background="Bisque" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="No" Binding="{Binding XPath=@Nickname}" /> <DataGridTextColumn Header="Sex" Binding="{Binding XPath=@Sex}" /> <DataGridTextColumn Header="Variety" Binding="{Binding XPath=@Variety}" /> </DataGrid.Columns> </DataGrid> <TextBlock Text="使用Mvvm的列子 方法三" FontWeight="Bold"></TextBlock> <DataGrid ItemsSource="{Binding Table1}" Background="Bisque" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="No" Binding="{Binding XPath=@No}" /> <DataGridTextColumn Header="Sex" Binding="{Binding XPath=@Sex}" /> <DataGridTextColumn Header="Weight" Binding="{Binding XPath=@Weight}"/> </DataGrid.Columns> </DataGrid> <DataGrid ItemsSource="{Binding Table2}" Background="Bisque" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="No" Binding="{Binding XPath=@Nickname}" /> <DataGridTextColumn Header="Sex" Binding="{Binding XPath=@Sex}" /> <DataGridTextColumn Header="Variety" Binding="{Binding XPath=@Variety}" /> </DataGrid.Columns> </DataGrid> </StackPanel> </Grid> </Window>
再看代码:
using GalaSoft.MvvmLight; using System.Windows.Data; using System.Xml; namespace MvvmLight1.viewmodel { public class Mainviewmodel : viewmodelBase { #region 属性 private string _welcomeTitle = string.Empty; public string WelcomeTitle { get { return _welcomeTitle; } set { _welcomeTitle = value; RaisePropertyChanged("WelcomeTitle"); } } private string xml_file_path = @"E:\DevApp\VisualStudio.NET2013.App\WPF\MvvmLight1\MvvmLight1\Model\database.xml"; public XmlDataProvider TigerXmlProvider { get { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(xml_file_path); XmlDataProvider xml_data_provider = new XmlDataProvider() { Document = doc,XPath ="/Database/Table1/Tiger" }; return xml_data_provider; } } public XmlDataProvider DogXmlProvider { get { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(xml_file_path); XmlDataProvider xml_data_provider = new XmlDataProvider() { Document = doc,XPath = "/Database/Table2/Dog" }; return xml_data_provider; } } public XmlDocument Database { get { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xml_file_path); return xmlDoc; } } public XmlNode Table1 { get { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xml_file_path); XmlNode root = xmlDoc.SelectSingleNode("Database"); XmlNode node = root.SelectSingleNode("Table1"); return node; } } public XmlNode Table2 { get { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xml_file_path); XmlNode root = xmlDoc.SelectSingleNode("Database"); XmlNode node = root.SelectSingleNode("Table2"); return node; } } #endregion /// <summary> /// Initializes a new instance of the Mainviewmodel class. /// </summary> public Mainviewmodel() { WelcomeTitle = "Welcome to MVVM Light"; } } }
<?xml version="1.0" encoding="utf-8"?> <Database Name="mydatabase" version="1.0"> <Table1 Name="Tiger"> <Tiger No="1" Sex="male" Weight="120"></Tiger> <Tiger No="2" Sex="female" Weight="80"></Tiger> <Tiger No="3" Sex="female" Weight="90"></Tiger> <Tiger No="4" Sex="male" Weight="110"></Tiger> </Table1> <Table2 Name="Dog"> <Dog Nickname="Peter" Sex="male" Variety="Shepherd Dog"></Dog> <Dog Nickname="Kaiser" Sex="male" Variety="Hunting Dog"></Dog> </Table2> </Database>
private string xml_file_path="**********";
运行结果:
参考:
原文链接:https://www.f2er.com/xml/296972.html