Object Data Sources bound to various data sources in WPF

Source: Internet
Author: User

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.

 

Ii. Bind WPF to object data sources of various data sources

Front-end code:

  <Window.Resources>        <Con:BackgroundConverter x:Key="BackgroundConverter"/>    </Window.Resources>

Bind an object data source with listview

<Listview Height = "262" margin =, 310 "verticalalignment =" bottom "itemssource =" {binding} "name =" listview2 "horizontalalignment =" right "width =" "> <listview. view> <gridview> <gridviewcolumn header = "no." displaymemberbinding = "{binding Path = ID}" width = "100"/> <gridviewcolumn header = "name" displaymemberbinding = "{ binding Path = Name} "width =" 100 "/> <gridviewcolumn header =" Age "width =" 100 "> <gridviewcolumn. celltemplate> <datatemplate> <textblock grid. column = "1" text = "{binding Path = age}" foreground = "{binding Path = age, converter = {staticresource backgroundconverter} "/> </datatemplate> </gridviewcolumn. celltemplate> </gridviewcolumn> </gridview> </listview. view> </listview>

  

Background code:

 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    }

Below is the person class

 public class Person    {        private string _ID;        private string _name;        private string _age;        public string ID        {            get;            set;        }        public string Name        {            get;            set;        }        public int Age        {            get;            set;        }    }

// The following figure shows the data source. Of course, the data source can be the data read from the database. You can create a data source directly here.

// Create a data source. You can also obtain the data source private ilist <person> getpersoninfo () {ilist <person> plist = new list <person> () from the database (); for (INT I = 40; I <60; I ++) {person P = new person (); p. id = "ID" + I; p. name = "nmae" + I; p. age = I + 1; plist. add (p) ;}return plist ;}public window1 () {initializecomponent (); listview2.datacontext = getpersoninfo ();}

:

2. Another way to use Object

    <Window.Resources>        <Con:BackgroundConverter x:Key="BackgroundConverter"/>        <ObjectDataProvider x:Key="myPerson" ObjectType="{x:Type Con:Person}"  MethodName="getPersonInfo"/>    </Window.Resources>

In this case, the myperson object data source is the getpersoninfo method in the person class.

([Supplement] If getpersoninfo is in different class libraries, use the following method:

   <ObjectDataProvider x:Key="myPerson2" ObjectType="{x:Type ClassLibrary1:Person}"  MethodName="getPersonInfo"/>

Classlibrary1 is the name of the class library. And add a namespace

 xmlns:ClassLibrary1="clr-namespace:ClassLibrary1;assembly=ClassLibrary1"

)

<Listview Height = "262" margin =, 310 "datacontext =" {staticresource myperson} "verticalignment =" bottom "itemssource =" {binding} "name =" listview2 "horizontalalignment =" right "width =" "> <listview. view> <gridview> <gridviewcolumn header = "no." displaymemberbinding = "{binding Path = ID}" width = "100"/> <gridviewcolumn header = "name" displaymemberbinding = "{ binding Path = Name} "width =" 100 "/> <gridviewcolumn header =" Age "width =" 100 "> <gridviewcolumn. celltemplate> <datatemplate> <textblock grid. column = "1" text = "{binding Path = age}" foreground = "{binding Path = age, converter = {staticresource backgroundconverter} "/> </datatemplate> </gridviewcolumn. celltemplate> </gridviewcolumn> </gridview> </listview. view> </listview>

  

 

 

 

 

 

  

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.