1. 程式人生 > >WPF應用Binding之ItemsSource

WPF應用Binding之ItemsSource

一、ListBox顯示簡單的列表資訊(未使用DataTemplate)

(1) Student類

    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int Id { get; set; }
    }

 (2) xaml
    <Grid>
        <StackPanel>
            <TextBlock Text="SelectID:" Background="Aquamarine"/>
            <TextBox x:Name="txtbox_SelectId"/>
            <TextBlock Text="Students:" Background="Aquamarine"/>
            <ListBox x:Name="listbox_Students"/>
        </StackPanel>
    </Grid>

(3) C#程式碼邏輯
    private void ShowStudent()
    {
        List<Student> studentList = new List<Student>()
        {
            new Student(){Id = 2003, Name = "tim", Age = 20},
            new Student(){Id = 2004, Name = "tom", Age = 21},
            new Student(){Id = 2005, Name = "toni", Age = 21},
            new Student(){Id = 2006, Name = "fidy", Age = 23},
            new Student(){Id = 2007, Name = "andy", Age = 22},
        };

        //ListBox資料繫結
        listbox_Students.ItemsSource = studentList;//資料來源
        listbox_Students.DisplayMemberPath = "Name";//ListBox只顯示Student的Name屬性

        //繫結被選中的id(2種方式均可)
#if true//方式1
        Binding binding = new Binding("SelectedItem.Id") { Source = listbox_Students };
#else//方式2
        Binding binding = new Binding();
        binding.Path = new PropertyPath("SelectedItem.Id");
        binding.Source = listbox_Students;
#endif
        txtbox_SelectId.SetBinding(TextBox.TextProperty, binding);
    }

二、ListBox顯示Student資訊(使用DataTemplate)

(1) Student類

    同上

(2) xaml

    <StackPanel>
        <TextBlock Text="SelectID:" Background="Aquamarine"/>
        <TextBox x:Name="txtbox_SelectId"/>
        <TextBlock Text="Students:" Background="Aquamarine"/>
        <ListBox x:Name="listbox_Students">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Id}" Width="40"/>
                        <TextBlock Text="{Binding Path=Name}" Width="60"/>
                        <TextBlock Text="{Binding Path=Age}" Width="40"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>

(3) C#程式碼邏輯
        private void ShowStudent()
        {
            List<Student> studentList = new List<Student>()
            {
                new Student(){Id = 2003, Name = "tim", Age = 20},
                new Student(){Id = 2004, Name = "tom", Age = 21},
                new Student(){Id = 2005, Name = "toni", Age = 21},
                new Student(){Id = 2006, Name = "fidy", Age = 23},
                new Student(){Id = 2007, Name = "andy", Age = 22},
            };

            listbox_Students.ItemsSource = studentList;//資料來源

            //繫結被選中的id(2種方式均可)
    #if true//方式1
            Binding binding = new Binding("SelectedItem.Id") { Source = listbox_Students };
    #else//方式2
            Binding binding = new Binding();
            binding.Path = new PropertyPath("SelectedItem.Id");
            binding.Source = listbox_Students;
    #endif
            txtbox_SelectId.SetBinding(TextBox.TextProperty, binding);