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>

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());
}
}
}

结果:

知识点:

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>

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=,Name="Tim",Age=},
new Employee(){Id=,Name="Tom",Age=},
new Employee(){Id=,Name="Guo",Age=}
}; 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;
}
}
}

结果:

知识点:

1)DisplayMemberPath设置要在ListBox上显示的属性

2)SelectedValuePath设置选中后的值的属性

3)ItemsSource设置数据源

一个ListBox的例子的更多相关文章

  1. 用一个简单的例子来理解python高阶函数

    ============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...

  2. Spring-Context之一:一个简单的例子

    很久之前就想系统的学习和掌握Spring框架,但是拖了很久都没有行动.现在趁着在外出差杂事不多,就花时间来由浅入深的研究下Spring框架.Spring框架这几年来已经发展成为一个巨无霸产品.从最初的 ...

  3. 高仿“点触验证码”做的一个静态Html例子

    先上源码: <html> <head> <title>TouClick - Designed By MrChu</title> <meta htt ...

  4. 关于apriori算法的一个简单的例子

    apriori算法是关联规则挖掘中很基础也很经典的一个算法,我认为很多教程出现大堆的公式不是很适合一个初学者理解.因此,本文列举一个简单的例子来演示下apriori算法的整个步骤. 下面这个表格是代表 ...

  5. 一个UWSGI的例子

    摘要:uwsgi执行顺序:启动master进程,执行python脚本的公共代码(import同一层).然后生成worker进程,uwsgi.post_fork_hook=init_functions, ...

  6. 扩展Python模块系列(二)----一个简单的例子

    本节使用一个简单的例子引出Python C/C++ API的详细使用方法.针对的是CPython的解释器. 目标:创建一个Python内建模块test,提供一个功能函数distance, 计算空间中两 ...

  7. fitnesse - 一个简单的例子(slim)

    fitnesse - 一个简单的例子(slim) 2017-09-30 目录1 编写测试代码(Fixture code)2 编写wiki page并运行  2.1 新建wikiPage  2.2 运行 ...

  8. Struts2的配置和一个简单的例子

    Struts2的配置和一个简单的例子 笔记仓库:https://github.com/nnngu/LearningNotes 简介 这篇文章主要讲如何在 IntelliJ IDEA 中使用 Strut ...

  9. 一个简单的例子搞懂ES6之Promise

    ES5中实现异步的常见方式不外乎以下几种: 1. 回调函数 2. 事件驱动 2. 自定义事件(根本上原理同事件驱动相同) 而ES6中的Promise的出现就使得异步变得非常简单.promise中的异步 ...

随机推荐

  1. javaWeb 在jsp中 使用自定义标签输出访问者IP

    1.java类,使用简单标签,jsp2.0规范, 继承 SimpleTagSupport public class ViewIpSimpleTag extends SimpleTagSupport { ...

  2. WKWebView新特性及JS交互

    引言 一直听说WKWebView比UIWebView强大许多,可是一直没有使用到,今天花了点时间看写了个例子,对其API的使用有所了解,为了日后能少走弯路,也为了让大家更容易学习上手,这里写下这篇文章 ...

  3. HDU 5996:dingyeye loves stone(阶梯博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5996 题意:在一棵树上进行博弈,每次只能将当前的结点的石子放到父节点上,最后不能移动的输. 思路:比赛的时候想的 ...

  4. HDU 3853:LOOPS(概率DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3853 LOOPS Problem Description   Akemi Homura is a M ...

  5. PostgreSQL中如何查看一个表所对应的文件

    通过pg_relation_filepath可以直接表(索引)对象对应的物理文件在哪里? 上面截图是“德哥”做的ppt:上面有详细解释! 当然也可以通过 系统表 pg_class 可以直接查出对应的物 ...

  6. [BIM]BIM中IFC介绍

    ifc是干什么的,看下图 ifc架构图 下文转自:http://www.bimcn.org/cjwt/201506053789.html IFC目前是国际通用的BIM标准,现在很多BIM软件都采用其作 ...

  7. getElementByClassName()不兼容的解决办法

    在获取元素时候采用getElementByClassName()方法是比较方便的,但是对于IE6不兼容,可以采用以下代码来自定义这个方法: window.onload=function(){ if(! ...

  8. python: hashlib 加密模块

    加密模块hashlib import hashlib m=hashlib.md5() m.update(b'hello') print(m.hexdigest()) #十六进制加密 m.update( ...

  9. word中设置前几页为罗马数字,后几页设置为阿拉伯数字

    假如第1-5页摘要部分页脚要是罗马数字,第6页开始是正文部分是阿拉伯数字,起始页为1. WORD2003 1.将光标定位在第5页末尾处,在菜单栏中依次点击“插入——分隔符——(分节符类型)下一页”.按 ...

  10. Sequence 分类: 栈和队列 2015-08-05 10:10 2人阅读 评论(0) 收藏

    Sequence Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8277 Accepted: 2708 Description ...