zoukankan      html  css  js  c++  java
  • 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数据,籍贯来自籍贯类。

  • 相关阅读:
    js中当call或者apply传入的第一个参数是null/undefined时,js函数内执行的上下文环境是什么?
    闭包的实现原理和作用以及堆栈溢出和内存泄漏原理和相应解决办法
    JavaScript的作用域和作用域链
    词法作用域和动态作用域
    理解 es6 中class构造以及继承的底层实现原理
    new一个对象的详细过程,手动实现一个 new操作符
    实现继承的几种方式以及他们的优缺点
    理解JavaScript的执行上下文栈,可以应用堆栈信息快速定位问题
    原型和原型链-instanceof的底层实现原理
    js判断变量未定义
  • 原文地址:https://www.cnblogs.com/linlf03/p/2168842.html
Copyright © 2011-2022 走看看