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; }
}
复制代码

 

以上,

posted on   白开水易拉罐  阅读(7962)  评论(3编辑  收藏  举报

编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
< 2012年9月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6

统计

点击右上角即可分享
微信分享提示