Implementation demo of multiple data sources in WPF

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.