一、场景
现在外部有一xml文件,我们想将该xml文件中的内容绑定到ListView中,该如何实现?
二、实现
xml文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?> <Persons> <Person id="1"> <Name>Mary</Name> <Age>26</Age> <FavorColor>Red</FavorColor> </Person> <Person id="2"> <Name>Mellisa</Name> <Age>27</Age> <FavorColor>Green</FavorColor> </Person> <Person id="3"> <Name>Tom</Name> <Age>28</Age> <FavorColor>Yellow</FavorColor> </Person> <Person id="4"> <Name>Jack</Name> <Age>26</Age> <FavorColor>Blue</FavorColor> </Person> <Person id="5"> <Name>Jim</Name> <Age>23</Age> <FavorColor>Pink</FavorColor> </Person> </Persons>
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace BindingDemo2XmlDataSource
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//XmlDocument doc = new XmlDocument();
//doc.Load(@"D:\MyStudy\Project\Practice\Practice\BindingDemo2XmlDataSource\Xml\Test.xml");
//XmlDataProvider provider = new XmlDataProvider();
//provider.Document = doc;
//provider.XPath = @"/Persons/Person";
XmlDataProvider provider = new XmlDataProvider();
provider.Source = new Uri(@"D:\MyStudy\Project\Practice\Practice\BindingDemo2XmlDataSource\Xml\Test.xml");
provider.XPath = @"/Persons/Person";
listView.DataContext = provider;
listView.SetBinding(ListView.ItemsSourceProperty, new Binding());
}
}
public class BackgroundConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string myColor = value.ToString();
SolidColorBrush myBrush = Brushes.Black;
switch(myColor)
{
case "Red":
myBrush = Brushes.Red;
break;
case "Green":
myBrush = Brushes.Green;
break;
case "Yellow":
myBrush = Brushes.Yellow;
break;
case "Blue":
myBrush = Brushes.Blue;
break;
case "Pink":
myBrush = Brushes.Pink;
break;
default:
break;
}
return myBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class WidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double myWidth = (double)value;
return (myWidth - 270) / 2;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
1 <Window x:Class="BindingDemo2XmlDataSource.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:BindingDemo2XmlDataSource" 7 mc:Ignorable="d" 8 Title="BindingDemo3XmlDataSource" Height="350" Width="525"> 9 <Grid> 10 <Grid.Resources> 11 <local:BackgroundConverter x:Key="backGroundCvt"></local:BackgroundConverter> 12 <local:WidthConverter x:Key="widthCvt"></local:WidthConverter> 13 14 </Grid.Resources> 15 <ListView Margin="10 20" Name="listView"> 16 <ListView.View> 17 <GridView> 18 <GridViewColumn Header="Id" DisplayMemberBinding="{Binding XPath=@id}" Width="50"> 19 20 </GridViewColumn> 21 <GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=Name}" Width="100"> 22 23 </GridViewColumn> 24 <GridViewColumn Header="Age" DisplayMemberBinding="{Binding XPath=Age}" Width="100"> 25 26 </GridViewColumn> 27 <GridViewColumn Header="FavorColor" Width="auto"> 28 <GridViewColumn.CellTemplate> 29 <DataTemplate > 30 <Grid> 31 <Grid.ColumnDefinitions> 32 <ColumnDefinition Width="*"/> 33 <ColumnDefinition Width="*"/> 34 </Grid.ColumnDefinitions> 35 <TextBlock Text="{Binding XPath=FavorColor}" 36 Grid.Column="0" 37 Width="{Binding ElementName=listView,Path=ActualWidth,Converter={StaticResource ResourceKey=widthCvt}}" 38 Foreground="{Binding XPath=FavorColor, Converter={StaticResource ResourceKey=backGroundCvt}}" 39 > 40 41 </TextBlock> 42 <TextBlock Grid.Column="1" 43 Background="{Binding XPath=FavorColor, Converter={StaticResource ResourceKey=backGroundCvt}}" 44 Width="{Binding ElementName=listView,Path=ActualWidth,Converter={StaticResource ResourceKey=widthCvt}}" 45 > 46 47 </TextBlock> 48 </Grid> 49 </DataTemplate> 50 </GridViewColumn.CellTemplate> 51 52 53 54 </GridViewColumn> 55 </GridView> 56 </ListView.View> 57 </ListView> 58 </Grid> 59 </Window>
运行结果如下:
注:绑定xml中的数据一定要用XPath而不是Path
Background="{Binding XPath=FavorColor, Converter={StaticResource ResourceKey=backGroundCvt}}"