现在的位置: 首页 > 综合 > 正文

WPF 多个数据源的实现DEMO

2012年03月31日 ⁄ 综合 ⁄ 共 2603字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.