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. 我是如何对网站CSS进行架构的

    by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=944 一.写在前面的 ...

  2. jsp页面的跳转取值

    <p >工单管理 >> <c:if test="${code eq 0}">全部工单>>详情页</c:if> <c ...

  3. access数据库导入Oracle

    1.对着当前的表右击->导出->选择下面的保存类型为"ODBC数据库"找一个路径输入文件名2.将x导出到x,点击->确定3.在弹出的对话框中DSN名称,点击-&g ...

  4. 理解Linux中断 (2)【转】

    转自:http://blog.csdn.net/tommy_wxie/article/details/7425692 版权声明:本文为博主原创文章,未经博主允许不得转载. .内核的中断处理 3.1.中 ...

  5. PBOC APDU命令解析【转】

    转自:http://blog.csdn.net/zuokong/article/details/49335257 版权声明:本文为博主原创文章,未经博主允许不得转载. 应用层发出的命令报文和卡片回送到 ...

  6. 表数据文件DBF的读取和写入操作

    import sys import csv import struct import datetime import decimal import itertools from cStringIO i ...

  7. Pending Statistics

    Starting with the 11g Release 1 (11.1), when gathering statistics, you have the option to automatica ...

  8. c#之线程池优先级

    using System; using System.Threading; namespace ConsoleApplication1 { class Program { static void Ma ...

  9. c#游戏 剪刀石头

    电脑using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace sh ...

  10. 20150603_Andriod 多个窗体数据回调

    package com.example.test1; import android.support.v7.app.ActionBarActivity;import android.os.Bundle; ...