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数据,籍贯来自籍贯类。

WPF 多个数据源的实现DEMO



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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
  • 2021-03-31
  • 2021-09-04
  • 2021-11-19
猜你喜欢
  • 2021-10-15
  • 2021-05-15
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案