WPF: dynamically add and delete listview Columns

Source: Internet
Author: User

 

Directory

  • Introduction
  • Usage
  • Source code

 

Returned directory

Introduction

Yesterday I wrote an article about sorting listview columns: a simple and easy-to-use method for sorting the columns clicked by WPF listview. Now let's take a look at how listview dynamically adds or deletes columns.

 

The effect is similar. You have defined a listview. You can use this method to dynamically generate a list of columns that can be modified based on the listview columns. By modifying the attributes of the list, listview columns can be dynamically added or deleted.

 

 

Returned directory

Usage

All functions of this method are implemented in one type: columnobject type.

First, you must useItemssourcefromcolumns of columnobjectAdditional attribute. The value requires a listview object, that is, the target listview for generating the column options. Of course, the premise is that the listview must have a column definition (the listview with gridviewcolumn ).

The additional attributes of itemssourcefromcolumns should be set in another itemscontrol (such as ListBox or listview). The generated column options will be displayed as the itemscontrol data source (itemssource attribute.

So to sum up the additional attributes of itemssourcefromcolumns: You need to set them to itemscontrol in a list of display column options. At the same time, the value of this attribute is the target listview that needs to generate dynamic column options!

 

After itemscontrol is modified by the itemssourcefromcolumns additional attribute of columnobject, The itemscontrol data source (I .e. the itemssource attribute) becomes a series of columnobject objects. Each columnobject represents a column of listview (gridviewcolumn object ). PassIsvisible of columnobjectThe dependency attribute is used to set whether the columns are visible in listview. That is to say, when the isvisible attribute of columnobject is changed, the corresponding columns in the target listview are automatically displayed or deleted.

At the same time, columnobject also has the column attribute (non-dependent attribute, because it is read-only and does not change internally, so it is not necessary to set it as a dependency attribute ), returns the gridviewcolumn object in the listview represented by columnobject.

 

The following code example:

For example, in listview, it has three columns: name, age, and score, which are respectively bound to the name, age, and score attributes of the Data Object (the code on the specific data is omitted ), in another itemscontrol (ListBox In the example), set the value of itemssourcefromcolumns, an additional attribute of the columnobject class, to this listview.

 

Finally, a checkbox control is displayed in the Data Template of ListBox (through the itemtemplate attribute), and the isvisible attribute of the Data Object columnobject is bound in both directions to dynamically control the display or deletion of the target listview column.

 

The following XAML code:

<Dockpanel>

<! -- Existing listview -->

<Listview name = "list" dockpanel. Dock = "TOP">

<Listview. View>

<Gridview>

<Gridviewcolumn header = "name"

Width = "100"

Displaymemberbinding = "{binding name}"/>

<Gridviewcolumn header = "Age"

Width = "50"

Displaymemberbinding = "{binding age}"/>

<Gridviewcolumn header = "score"

Width = "100">

<Gridviewcolumn. celltemplate>

<Datatemplate>

<Progressbar width = "80" Height = "10" Maximum = "100" value = "{binding score}"/>

</Datatemplate>

</Gridviewcolumn. celltemplate>

</Gridviewcolumn>

</Gridview>

</Listview. View>

</Listview>

 

<! -- Column management code -->

<! -- Loc namespace is the namespace where the columnobject type definition is located -->

<ListBox LOC: columnobject. itemssourcefromcolumns = "{binding elementname = List}">

<ListBox. itemcontainerstyle>

<Style targettype = "listboxitem">

<Setter property = "focusable" value = "false"/>

</Style>

</ListBox. itemcontainerstyle>

<ListBox. itemtemplate>

<Datatemplate>

<Checkbox ischecked = "{binding isvisible }"

Content = "{binding column. header }"

Margin = "2"/>

</Datatemplate>

</ListBox. itemtemplate>

</ListBox>

</Dockpanel>

 

The running result is similar.

 

 

Returned directory

Source code

You can download the source code project:

Project download

Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.

Environment: Visual C #2008 Express

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.