• WPF中使用ItemsControl嵌套绑定,在ItemsControl中嵌套一个ItemsControl,然后使用绑定


    最需要注意的一点是,绑定一定要使用属性,因为属性提供{set;get;}方法。

    XAML中的定义:

    注意:需要在第二层ItemsControl的ItemsSource绑定的内容

    <Window x:Class="Binding_Demo_01.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">
        <Grid>
            <ItemsControl x:Name="list1">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" Margin="10" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding CurrPerson}" MouseDoubleClick="ItemsControl_MouseDoubleClick">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" Margin="10" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
                                        <Image Source="{Binding Image}" Stretch="UniformToFill" Height="400" Width="230" Margin="4"/>
                                        <TextBlock Text="{Binding Name}" Margin="4"/>
                                    </StackPanel>
                                </DataTemplate>
                             </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </Window>

    CS文件的内容:

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                ObservableCollection<Persons> persons = new ObservableCollection<Persons>
                {
                    new Persons
                    {
                        CurrPerson = new List<Person>
                        {
                            new Person{Name="Chrysanthemum", Age=21, Email="chrysanthemum@gmail.com", Image="Chrysanthemum.jpg"},
                            new Person{Name="Desert", Age=23, Email="Desert@gmail.com", Image="Desert.jpg"}
                        }
                    },
    
                    new Persons
                    {
                        CurrPerson = new List<Person>
                        {
                            new Person{Name="Jellyfish", Age=32, Email="Jellyfish@gmail.com", Image="Jellyfish.jpg"},
                            new Person{Name="Hydrangeas", Age=23, Email="Hydrangeas@gmail.com", Image="Hydrangeas.jpg"}
                            }
                    }
                };
    
                list1.ItemsSource = persons;
            }
    
            private void ItemsControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
    
            }
        }

    第三部分:

    Person类的定义

    class Person
    {
        public string Name { get; set; }
        public int Age { set; get; }
        public string Image { set; get; }
        public string Email { set; get; }
    }
    
    class Persons
    {
        public List<Person> CurrPerson { set; get; }
    }

    以上,

  • 相关阅读:
    Application.Current的使用
    .NET中资源文件的使用
    PMP模拟试题与解析(七)
    PMP模拟试题与解析(四)
    RMAN命令简介
    数据库备份和恢复概述
    ORA-14402: updating partition key column would cause a partition change
    RMAN概述
    PLS-00642: local collection types not allowed in SQL statements
    SFTP(Secure File Transfer Protocol)安全的文件传输协议的使用
  • 原文地址:https://www.cnblogs.com/leelike/p/2683748.html
Copyright © 2020-2023  润新知