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 2012-09-13 17:38  白开水易拉罐  阅读(7930)  评论(3编辑  收藏  举报