WPF 多个数据源的实现DEMO

WPF 多个数据源的实现DEMO,ListView中有个Combox籍贯,ListView的数据来自XML数据源,Combox籍贯来自另一个数据源。

<ListView Height= "262"  Margin= "345,12,12,0"  ItemsSource= "{Binding Source={StaticResource myPerson4},XPath=/PersonF/person}"  VerticalAlignment= "Top"   Name= "listView3" >
        <ListView.Resources>
            <Con:JiGuan x:Key= "JG" />
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Header= "编号"  DisplayMemberBinding= "{Binding XPath=ID}"   Width= "100"  />
                <GridViewColumn Header= "姓名"  DisplayMemberBinding= "{Binding XPath=Name}"  Width= "100" />
                <GridViewColumn Header= "年龄"  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>

资源:

<Window.Resources>
        <Con:BackgroundConverter x:Key= "BackgroundConverter" />
         <XmlDataProvider x:Key= "myPerson4"  Source= "/Person.xml" />
    </Window.Resources>

值转换:

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
  }

籍贯类:  

public  class  JiGuan
{
     private  ObservableCollection< string > _jiguan;
     public  JiGuan()
     {
         _jiguan = new  ObservableCollection< string >();
         _jiguan.Add( "上海" );
         _jiguan.Add( "杭州" );
         _jiguan.Add( "南京" );
         _jiguan.Add( "广州" );
     }
 
     public  ObservableCollection< string > JGuan
     {
         get  { return  new  ObservableCollection< string >(_jiguan); }
         private  set  { }
     }
 
}

  

XML文件

<?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>

  效果图:如下图,编号,姓名,年龄,籍贯来自不同的数据源,但是却在同一个ListView中,编号,姓名,年龄来自XML数据,籍贯来自籍贯类。



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/09/06/2168842.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值