zoukankan      html  css  js  c++  java
  • 一个ListBox的例子

    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设置数据源

  • 相关阅读:
    2020_java面试
    Centos7.2 安装docker、mysql和redis
    【源码讲解】Spring事务是如何应用到你的业务场景中的?
    奇葩说今晚聊前任|扒一扒你的前任企业邮箱
    一封一封邮件发送太累?个人邮箱快速群发解决烦恼
    企业邮箱客户端收发服务器如何设置?
    163VIP邮箱怎么设置邮件签名?如何群发邮件?
    企业版邮箱哪个适合学校邮箱?企业邮箱托管服务
    企业版邮箱购买哪个?公司邮箱如何申请?
    公司邮箱登录入口是?公司邮箱申请入口在?
  • 原文地址:https://www.cnblogs.com/KeenLeung/p/3523135.html
Copyright © 2011-2022 走看看