WPF綁定各種數據源之xml數據源
阿新 • • 發佈:2017-07-07
convert 使用 spa 代碼 win name uri mss 使用外部
一、WPF綁定各種數據源索引
WPF 綁定各種數據源之Datatable
WPF綁定各種數據源之object數據源
WPF綁定各種數據源之xml數據源
WPF綁定各種數據源之元素控件屬性
Binding的基礎可參考WPF 綁定基礎
二、WPF綁定各種數據源之xml數據源,此時的XML源寫在界面了,當然也可以獨立成文件。
<Window.Resources>
<Con:BackgroundConverter x:Key= "BackgroundConverter" />
<XmlDataProvider x:Key= "myPerson3" >
<x:XData>
<PersonF xmlns= "" >
<person Name= "Person1" >
<ID>1</ID>
<Name>XiaoA</Name>
<Age>49</Age>
</person> <person Name= "Person2" >
<ID>2</ID>
<Name>XiaoB</Name>
<Age>29</Age>
</person>
<person Name= "Person3" >
<ID>3</ID>
<Name>XiaoC</Name>
<Age>103</Age>
</person>
<person Name= "Person4" >
<ID>4</ID>
<Name>XiaoD</Name>
<Age>59</Age>
</person>
</PersonF>
</x:XData>
</XmlDataProvider>
</Window.Resources>
|
下面是綁定的代碼。此時需要註意,原來用Path改成了XPath,因為這是XML源,並且ItemsSource 改成 ItemsSource="{Binding Source={StaticResource myPerson3},XPath=/PersonF/person}"
<ListView Height= "262" Margin= "0,32,56,0" ItemsSource= "{Binding Source={StaticResource myPerson3},XPath=/PersonF/person}" VerticalAlignment= "Top" Name= "listView3" HorizontalAlignment= "Right" Width= "310" >
<ListView.View>
<GridView>
<GridViewColumn Header= "編號" DisplayMemberBinding= "{Binding XPath=ID}" Width= "100" />
<GridViewColumn Header= "姓名" DisplayMemberBinding= "{Binding XPath=Name}" Width= "100" />
<GridViewColumn Header= "年齡" Width= "100" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Grid.Column= "1" Text= "{Binding XPath=Age}" Foreground= "{Binding XPath=Age, Converter={StaticResource BackgroundConverter}}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
|
下面是值轉換
public class BackgroundConverter : IValueConverter
{
#region IValueConverter Members
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Color color = new Color();
int num = int .Parse(value.ToString());
if (num > 100)
color = Colors.Yellow;
else if (num < 50)
color = Colors.LightGreen;
else
color = Colors.LightPink;
return new SolidColorBrush(color);
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
|
效果圖:
2.使用外部XML數據源,只需做如下修改
<Window.Resources>
<XmlDataProvider x:Key= "myPerson3" Source= "/Person.xml" />
</Window.Resources>
|
3.如果使用外部XML數據源,並且采用C#代碼的形式,則如下:
XmlDocument doc = new XmlDocument();
doc.Load( @"http://www.cnblogs.com/XMLFile1.xml" );
XmlDataProvider provider = new XmlDataProvider();
provider.Document = doc;
provider.XPath = @"/PersonF/person" ;
listView3.DataContext = provider;
listView3.SetBinding(ListView.ItemsSourceProperty, new Binding());
|
當然也可以使用XMLDataProvider的Source屬性,此時只需做如下修改:
XmlDataProvider provider = new XmlDataProvider();
provider.Source = new Uri( @"F:\\XMLFile1.xml" );
provider.XPath = @"/PersonF/person" ;
|
其他不變。
作者:Work Hard Work Smart
出處:http://www.cnblogs.com/linlf03/
歡迎任何形式的轉載,未經作者同意,請保留此段聲明!
WPF綁定各種數據源之xml數據源