The implementation demo of multiple data sources in WPF. listview has a combox place. listview data comes from the XML data source and combox comes from another data source.
<Listview Height = "262" margin = "345,12," itemssource = "{binding source = {staticresource myperson4 }, XPath =/personf/person} "verticalignment =" TOP "name =" listview3 "> <listview. resources> <con: jiguan X: Key = "JG"/> </listview. resources> <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> <gridviewcolumn header = "" width = "100"> <gridviewcolumn. celltemplate> <datatemplate> <ComboBox datacontext = "{binding source = {staticresource JG}" width = "50" selectedindex = "0"> <itemscontrol itemssource = "{binding jguan} "> <itemscontrol. itemtemplate> <datatemplate> <textblock text = "{binding}"/> </datatemplate> </itemscontrol. itemtemplate> </itemscontrol> </ComboBox> </datatemplate> </gridviewcolumn. celltemplate> </gridviewcolumn> </gridview> </listview. view> </listview>
Resource:
<Window.Resources> <Con:BackgroundConverter x:Key="BackgroundConverter"/> <XmlDataProvider x:Key="myPerson4" Source="/Person.xml"/> </Window.Resources>
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 }
Nationality:
Public class jiguan {private observablecollection <string> _ jiguan; Public jiguan () {_ jiguan = new observablecollection <string> (); _ jiguan. add ("Shanghai"); _ jiguan. add ("Hangzhou"); _ jiguan. add ("Nanjing"); _ jiguan. add ("Guangzhou");} public observablecollection <string> jguan {get {return New observablecollection <string> (_ jiguan);} private set {}}}
XML file
<?xml version="1.0" encoding="utf-8" ?><PersonF xmlns=""> <person Name="Person1"> <ID>1</ID> <Name>XiaoA</Name> <Age>59</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>
: For example, number, name, age, and nationality come from different data sources, but in the same listview, the number, name, and age come from XML data, and the nationality comes from the nationality class.