• WPF之Binding基础五 使用集合对象作为列表控件的ItemSource


      我们知道,C#里面所有可以被迭代便利的集合都继承了IEnumerable接口,WPF中的列表控件都继承了ItemsControl这个类,所有就有ItemSource这个属性,而ItemSource是可以接受一个派生自IEnumerable这个接口的实例作为自己的直。所以说ListBox就有ListBoxItem这个跳模容器。当我们为一个ItemControl对象设置了ItemSource的时候,他就会自己去循环其中的数据元素。

      ItemTemplate属性的类型三DataTemplate,因为继承制ItemControl这个类(事例二就很好的体现了)

    XAML代码

    <Window x:Class="使用集合对象作为列表控件的ItemSource.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <!--案例一-->
    <!--<StackPanel x:Name="stackpanel" Background="LightBlue">
    <TextBlock Text="Age" FontWeight="Bold" Margin="5"/>
    <TextBox x:Name="txtAge" Margin="5"/>
    <TextBlock Text="List" FontWeight="Bold" Margin="5"/>
    <ListBox x:Name="List" Height="150" Margin="5"/>
    </StackPanel>-->
    <!--案例二-->
    <StackPanel x:Name="stackpanel" Background="LightBlue">
    <TextBlock Text="Age" FontWeight="Bold" Margin="5"/>
    <TextBox x:Name="txtAge" Margin="5"/>
    <TextBlock Text="List" FontWeight="Bold" Margin="5"/>
    <ListBox x:Name="List" Height="150" Margin="5">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <!--Orientation="Horizontal"水平显示-->
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Path=Age}" Width="30"/>
    <TextBlock Text="{Binding Path=Name}" Width="30"/>
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
    </StackPanel>
    </Window>

    CS代码

    namespace 使用集合对象作为列表控件的ItemSource
    {
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    //方案一
    //一个简单的list集合
    List<User> list = new List<User>()
    {
    new User(){Age=18,Name="张三"},
    new User(){Age=20,Name="李氏"},
    new User(){Age=19,Name="张五"},
    new User(){Age=29,Name="李广"},
    new User(){Age=36,Name="张笑话"},
    new User(){Age=25,Name="李小天"},
    };

    ObservableCollection<User> oc = new ObservableCollection<User>()
    {
    new User(){Age=18,Name="张三"},
    new User(){Age=20,Name="李氏"},
    new User(){Age=19,Name="张五"},
    new User(){Age=29,Name="李广"},
    new User(){Age=36,Name="张笑话"},
    new User(){Age=25,Name="李小天"},
    };

    //为listbox设置属于源,数据源为list
    //this.List.ItemsSource = list;
    //因为ObservationCollection实现了INotifyCollectionChanged和INotifyPropertyChanged接口,能把集合的变化及时的显示出来,推荐用
    //数据源为oc的时候
    //this.List.ItemsSource = oc;
    //this.List.DisplayMemberPath = "Name";

    //Binding binding = new Binding("SelectedItem.Age") {Source = this.List};
    //this.txtAge.SetBinding(TextBox.TextProperty, binding);


    //方案二
    this.List.ItemsSource = oc;
    Binding binding = new Binding("SelectedItem.Age") { Source = this.List };
    this.txtAge.SetBinding(TextBox.TextProperty, binding);
    }
    }
    }

    User类

    namespace 使用集合对象作为列表控件的ItemSource
    {
    class User
    {
    public int Age { get; set; }
    public string Name{ get; set; }
    }
    }

  • 相关阅读:
    HBase HTablePool
    Hadoop MapReduce InputFormat/OutputFormat
    MapReduce执行过程源码分析(一)——Job任务的提交
    HBase MultiVersionConsistencyControl
    HBase Split
    HBase HFile
    Do not to test a private method.
    HTML5使用ApplicationCache
    Swift使用FMDB操作SQLite
    使用Swift操作NSDate类型基础
  • 原文地址:https://www.cnblogs.com/lijin/p/3143228.html
Copyright © 2020-2023  润新知