一、WPF绑定各种数据源索引

WPF 绑定各种数据源之Datatable

WPF绑定各种数据源之object数据源

WPF绑定各种数据源之xml数据源

WPF绑定各种数据源之元素控件属性

Binding的基础可参考WPF 绑定基础

 

 

二、WPF绑定各种数据源之object数据源

前台代码:

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

  用ListView绑定object数据源

<ListView Height="262" Margin="0,0,45,51" VerticalAlignment="Bottom" ItemsSource="{Binding}" Name="listView2" HorizontalAlignment="Right" Width="310">
          <ListView.View>
              <GridView>
                  <GridViewColumn Header="编号" DisplayMemberBinding="{Binding Path=ID}"  Width="100" />
                  <GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Path=Name}" Width="100"/>
                  <GridViewColumn Header="年龄" 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>

  

后台代码:

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
   }

  下面是Person类

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;
       }
   }

  //下面是获得数据源,当然数据源可以是数据库中读出的数据,此处直接创建数据源

//创建数据源,当然也可以从数据库中获得数据源
private IList<Person> getPersonInfo()
  {
      IList<Person> pList = new List<Person>();
      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. 使用Object的另一种方式

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

 此时的myPerson对象数据源在是Person类中的getPersonInfo方法。

 (【补充】如果getPersonInfo在不同的类库中,则用如下方式

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

  使用,ClassLibrary1是类库的名称。并添加命名空间

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

  ) 

<ListView Height="262" Margin="0,0,45,51" DataContext="{StaticResource myPerson}" VerticalAlignment="Bottom" ItemsSource="{Binding}" Name="listView2" HorizontalAlignment="Right" Width="310">
           <ListView.View>
               <GridView>
                   <GridViewColumn Header="编号" DisplayMemberBinding="{Binding Path=ID}"  Width="100" />
                   <GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Path=Name}" Width="100"/>
                   <GridViewColumn Header="年龄" 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>

  http://www.cnblogs.com/linlf03/archive/2011/09/06/2168598.html