1. WPFBind various data source Indexes
Datatable for binding various data sources to WPF
Object Data Sources bound to various data sources in WPF
XML data sources bound to various data sources in WPF
Element control attributes bound to various data sources in WPF
For details about binding, refer to WPF binding basics.
2. WPF binds XML data sources of various data sources. The XML source is written on the interface, and can also be independently written into files.
<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>
The following is the bound code. In this case, you must note that the path is changed to XPath because it is an XML source and itemssource is changed to itemssource = "{binding source = {staticresource myperson3}, XPath =/personf/person }"
<Listview Height = "262" margin = "," itemssource = "{binding source = {staticresource myperson3 }, XPath =/personf/person} "verticalignment =" TOP "name =" listview3 "horizontalalignment =" right "width =" 310 "> <listview. view> <gridview> <gridviewcolumn header = "no." displaymemberbinding = "{binding XPath = ID}" width = "100"/> <gridviewcolumn header = "name" displaymemberbinding = "{ binding XPath = Name} "width =" 100 "/> <gridviewcolumn header =" Age "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>
Below is the value conversion
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. To use an external XML data source, make the following modifications:
<Window.Resources> <XmlDataProvider x:Key="myPerson3" Source="/Person.xml"/> </Window.Resources>
3. If the external XML data source is used and the C # code is used, the following code is used:
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());
Of course, you can also use the source attribute of xmldataprovider. You only need to make the following modifications:
XmlDataProvider provider = new XmlDataProvider(); provider.Source = new Uri(@"F:\\XMLFile1.xml"); provider.XPath = @"/PersonF/person";
Others remain unchanged.