`

使用Items属性

阅读更多
随 WPF 附带的每个 ItemsControl 具有一个对应的类,该类代表 ItemsControl 中的一个项。下表列出了随 WPF 附带的 ItemsControl 对象及其相应的项容器。

ItemsControl  项容器

ComboBox   ComboBoxItem

ContextMenu  MenuItem

ListBox   ListBoxItem

ListView  ListViewItem

Menu    MenuItem

StatusBar  StatusBarItem

TabControl  TabItem

TreeView  TreeViewItem

<Window x:Class="WpfApplication1.使用Items属性"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="使用Items属性" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="82*" />
            <RowDefinition Height="96*" />
            <RowDefinition Height="84*" />
        </Grid.RowDefinitions>

        <!--方法一-->
        <ListBox Grid.Row="0">
            <ListBox.Items>
                <ListBoxItem>
                    Item A  
                </ListBoxItem>
                <ListBoxItem>
                    <TextBlock Text="Item B" /> 
                </ListBoxItem>
            </ListBox.Items>
        </ListBox>
       
        <!--方法二,省略ListBox.Items-->
        <ListBox Grid.Row="1">
            <ListBoxItem>
                Item A
            </ListBoxItem>
            <ListBoxItem>
                <TextBlock Text="Item B" /> 
            </ListBoxItem>
        </ListBox>
       
        <!--方法三,省略ListBoxItem-->
        <ListBox Grid.Row="2">
            <ListBox.Items>
                Item A 
                <TextBlock Text="Item B" />
            </ListBox.Items>
        </ListBox>
    </Grid>
</Window>

四、HeaderedItemsControl模型
HeaderedItemsControl 从 ItemsControl 类继承。HeaderedItemsControl 定义 Header 属性,该属性遵从相同的规则,因为 HeaderedContentControl. WPF 的 Header 属性附带三个从 HeaderedItemsControl 继承的控件:MenuItem、ToolBar、TreeViewItem

HeaderedItemsControl模型可以理解为如下结构:一个HeaderedItemsControl包含一个Items集合,每一个Item包含一个Header属性,一个子Items集合,以TreeView和TreeViewItem为例:
<Window x:Class="WpfApplication1.HeaderedItemsControl模型"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="HeaderedItemsControl模型" Height="300" Width="300">
    <Canvas>
        <TreeView>
            <TreeView.Items>
                <TreeViewItem>
                    <TreeViewItem.Header>
                        <TextBlock Text="Root Node A" />
                        </TreeViewItem.Header>
                    <TreeViewItem.Items>
                        <TextBlock Text="A - 1" />      
                            <TreeViewItem> 
                                <TreeViewItem.Header> 
                                    <TextBlock Text="A - 2" />
                                </TreeViewItem.Header> 
                                <TreeViewItem.Items>
                                    <TextBlock Text="A - 2 - 1" /> 
                                    <TextBlock Text="A - 2 - 2" /> 
                                    <TextBlock Text="A - 2 - 3" />
                                </TreeViewItem.Items> 
                            </TreeViewItem> 
                        </TreeViewItem.Items>
                </TreeViewItem>
                <TreeViewItem>
                    <TreeViewItem.Header>Tree Node B</TreeViewItem.Header>
                    <TreeViewItem.Items>
                        <TextBlock>B-1</TextBlock>
                        <TreeViewItem>
                            <TreeViewItem.Header>
                                B_B_1
                            </TreeViewItem.Header>
                            <TextBlock>B-B_1</TextBlock>
                            <TextBlock>B-B_2</TextBlock>
                        </TreeViewItem>
                    </TreeViewItem.Items>
                   
                </TreeViewItem>
            </TreeView.Items>
        </TreeView>
    </Canvas>
</Window>



分享到:
评论

相关推荐

    ControlItems.zip

    上次上传的大家说有问题,是我没演示怎么使用items属性,我进行了改进, 大家可以看看类似datagrid的列属性items是怎么实现的。代码注视完整,开发平台vs2005,代码语言C#。

    wpf经典教程(pdf)

    2、使用Items属性........................................................ 40 四、HeaderedItemsControl模型............................................. 42 Panel Decorator TextBlock内容模型.................

    WPF经典教程 pdf

    2、使用Items属性 ................................................................................... 四、HeaderedItemsControl模型 .........................................................................

    wpf基础、wpf教程

    1、可以使用Orientation 属性更改堆叠的顺序 ................................................................................... 19 2、设置控件的属性,调整控件的显示........................................

    WPF教程 pdf

    2、使用Items属性 ................................................................................... 四、HeaderedItemsControl模型 ........................................................................

    WPF经典教程

    19 1、可以使用Orientation属性更改堆叠的顺序 ................................................................................... 19 2、设置控件的属性,调整控件的显示......................................

    Delphi 修改内存保护属性.rar

     ListBox1.Items.Add('所分配保护属性: ' DisplayProtections(MemInfo.AllocationProtect));  ListBox1.Items.Add('访问的保护属性: ' DisplayProtections(MemInfo.Protect));  case MemInfo.State of  MEM_...

    visual c#.net 2008控件使用范例大全

    2.3.10 Items属性 2.3.11 MaximumSize和MinimumSize属性 2.3.12 MultiColumn属性 2.3.13 RightToLefl属性 2.3.14 ScrollAlwaysVisible属性 2.3.15 SelectionMode属性 2.3.16 Sorted属性 2.3.17 ThreeDCheckBoxes属性...

    使用Java内部类实现一个简单的购物车系统.txt

    首先定义了一个ShoppingCart类,其中有一个List类型的items属性和一个double类型的totalPrice属性。在ShoppingCart类的构造方法中初始化了items属性。然后定义了addItem()方法和removeItem()方法,分别用来向items...

    动画演示flex属性 - 可点击切换属性

    align-items 单行盒子在侧轴上的对齐方式 flex-start:侧轴开始 flex-end:侧轴结束 center:居中 stretch:拉伸(默认值) 四. justify-content 子盒子主轴方向上的对齐方式 等,字数不够,无法一一列举

    Tensorflow-Tensor(对Tensor flow中使用函数的详细介绍)

    对Tensor flow中使用函数的详细介绍,内包含代码实例,还有详细讲解,运行结果

    Visual C# 2008控件\C#.net 2008例子(11.12)\第2章

    2.3.10 Items属性 2.3.11 MaximumSize和MinimumSize属性 2.3.12 MultiColumn属性 2.3.13 RightToLefl属性 2.3.14 ScrollAlwaysVisible属性 2.3.15 SelectionMode属性 2.3.16 Sorted属性 2.3.17 ThreeDCheckBoxes属性...

    第8章 JSTL.ppt

    forEach标签可以将集合中的成员遍历一遍,通过items属性指定一个集合,var属性指明集合中的一个成员,可以通过var属性指定的变量来访问集合中的成员。 实例 爱好:&lt;c:forEach items="${paramValues.Habit}" var=...

    delphi资源-Treeview的使用介绍

     每一个节点下子节点形成这一节点的Items属性,当前节点有一个唯一的Index(TreeNode的Index属性),用于说明子节点 在Items中的位置,每一个节点下的子节点是顺序编号的,第一个是0,第二个是1,依次类推。...

    C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法

    由于毕业后工作没有对接到专业问题,导致四五年没有碰过Winform程序了。...cbTcid.Items.Clear(); cbTcid.DataSource = CustomerBLL.select(); cbTcid.ValueMember = Cid; cbTcid.DisplayMember = Cpname; } else { ret

    C#入门教程之ListBox控件使用方法

    ListBox控件的使用: 1)控件属性 Items SelectedItems SelectioModes 2)数据绑定 DataSoure DisplayMember ValueMenber 3)实例 下面开始一一说明上面的ListBox控件的使用。 首先来说控件的属性, (1)Items:使用此...

    android ListView 一些重要属性详解

    第二是transciptMode属性,需要用ListView或者其它显示大量Items的控件实时跟踪或者查看信息,并且希望最新的条目可以自动滚动到可视范围内。通过设置的控件transcriptMode属性可以将Android平台的控件(支持...

    Delphi控件大全.doc

    重要属性items.* 举例,界面设计如下左,执行结果如下右: 代码为 procedure TForm1.Button1Click(Sender: TObject); begin edit1.Text := listbox1.Items.CommaText; edit2.Text := listbox1.Items.Text; edit3....

    VB.NET 2008 自定义(彩票矩阵)控件

    语言:VB.NET ... 没有对弹出的类似Items对话框进行自定义设置及实现两个自定义属性值之间的关联。即选择项目中aMatrix的值未默认值的时候,如何屏蔽ButtonItems设置功能。 欢迎各位相互交流,QQ86150045

    serverkit-login_items:Mac OS X登录项Serverkit插件

    serverkit-login_items ...属性 path - 登录项的路径(必填) hidden - 是否隐藏(默认: false ) 例子 resources : - id : alfred_2 type : login_items path : ' /Applications/Alfred 2.app ' hidden : true

Global site tag (gtag.js) - Google Analytics