文章出處

1.向ListBox中放入其他控件

XAML:

<Window x:Class="ItemsControls.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="184.328" Width="160.821">
    <Grid>
        <Grid>
            <ListBox Margin="5">
                <CheckBox x:Name="checkBoxTim" Content="Tim"></CheckBox>
                <CheckBox x:Name="checkBoxTom" Content="Tom"></CheckBox>
                <CheckBox x:Name="checkBoxBruce" Content="Bruce"></CheckBox>
                <Button x:Name="buttonMess" Content="Mess"></Button>
                <Button x:Name="buttonOwen" Content="Owen"></Button>
                <Button x:Name="buttonVictor" Content="Victor" Click="buttonVictor_Click"></Button>
            </ListBox>
        </Grid>
    </Grid>
</Window>
View Code

 

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ItemsControls
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 找出父級容器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonVictor_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            DependencyObject level1 = VisualTreeHelper.GetParent(btn);
            DependencyObject level2 = VisualTreeHelper.GetParent(level1);
            DependencyObject level3 = VisualTreeHelper.GetParent(level2);
            MessageBox.Show(level1.GetType().ToString());
            MessageBox.Show(level2.GetType().ToString());
            MessageBox.Show(level3.GetType().ToString());
        }
    }
}
View Code

 

結果:

 

知識點:

1)ListBox的父級容器是ListBoxItem

2)把數據集交給ListBox后,無需標明ListBoxItem,它會自動包裝整的

3)VisualTreeHelper類提供一些實用工具方法,用于執行涉及可視化樹中的節點的常規任務。

 

2.放入其他數據

XAML:

<Window x:Class="ItemsControls.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <Grid>
        <Grid>
            <ListBox x:Name="listBoxEmployee"></ListBox>
        </Grid>
    </Grid>
</Window>
View Code

 

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ItemsControls
{
    /// <summary>
    /// Window1.xaml 的交互邏輯
    /// </summary>
    public partial class Window1 : Window
    {
        List<Employee> empList = new List<Employee>(){
            new Employee(){Id=1,Name="Tim",Age=21},
            new Employee(){Id=2,Name="Tom",Age=22},
            new Employee(){Id=3,Name="Guo",Age=23}
        };

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.listBoxEmployee.DisplayMemberPath = "Name";//設置在ListBox上顯示的屬性
            this.listBoxEmployee.SelectedValuePath = "Id";//設置ListBox選中后的屬性值
            this.listBoxEmployee.ItemsSource = empList;
        }
    }
}
View Code

 

結果:

知識點:

1)DisplayMemberPath設置要在ListBox上顯示的屬性

2)SelectedValuePath設置選中后的值的屬性

3)ItemsSource設置數據源

 

 

 

 

 

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()