WPF paging DataGrid (ii) Column right-click menu implementation

Source: Internet
Author: User
Tags visibility

The "WPF paging DataGrid" describes how to implement paging functionality, which is described in this article if you implement the right-click menu. See figure below:

Click on the menu item age, hide the Age column, and then click the display, and implement the Move column, synchronized display.

To implement a feature you need to add a right-click menu to the column when loaded events.

public class Pagingdatagrid:datagrid {
	...
	void Pagingdatagrid_loaded (object sender, RoutedEventArgs e) {
		raisepagechanged ();
		Sets the right key menu for the column
		Setcolumncontextmenu ();
        }

	void Setcolumncontextmenu () {

	}
}


See Setcolumncontextmenu Method:
Enumerate all the columns, and then create the same menu item, whether the column will be bound to the menu item is checked, it needs to use code binding, the right key event to determine whether it is Datagridcolumnheader, is to display the right button menu. Columnreordering and Columnreordeed are the events that move the columns, not the same, but the order of the menu items is different from the order in which they were actually moved.

void Setcolumncontextmenu () {ContextMenu cm = new ContextMenu ();
	Visibility2booleanconverter converter = new Visibility2booleanconverter (); foreach (DataGridColumn col in this. Columns) {MenuItem mi = new MenuItem {Header = col.
		Header}; Binding ischeckedbinding = new Binding {path=new PropertyPath ("Visibility"), Source=col, Mode=bindingmode.twoway
		, Converter=converter,}; Mi.
		SetBinding (Menuitem.ischeckedproperty, ischeckedbinding); Mi. Click + = (s, e) => {mi. ischecked =!mi.
		ischecked;
		}; Cm.
	Items.Add (MI); //Right key column name, triggering right button menu to show this.
		Mouserightbuttondown + = (s, e) => {DependencyObject obj = E.originalsource as DependencyObject; while (obj!= null &&!) (
		obj is Datagridcolumnheader)) {obj = visualtreehelper.getparent (obj); } if (obj!= null && obj is Datagridcolumnheader) {(datagridcolumnheader) obj).
		ContextMenu = cm;

	}
	};
	int startcolumnindex = 0, endcolumnindex = 0;
	Object Tmpmenuitem=null; This. ColumnreordeRing + + (S, e) => {startcolumnindex = E.column.displayindex; Tmpmenuitem = cm.
		Items[startcolumnindex]; Cm.
	Items.removeat (Startcolumnindex);
	}; This.
		Columnreordered + = (s, e) => {endcolumnindex = E.column.displayindex; Cm.
	Items.insert (Endcolumnindex, Tmpmenuitem);
};
 }


In which, you need to build a converter class, the role is to convert visibility to bool type

<summary>
///Visibility to Boolean conversion class
///</summary> public
class Visibility2booleanconverter:ivalueconverter {Public
	object Convert (object value, Type targettype, Object parameter, System.Globalization.CultureInfo culture) {return
		(Visibility) value = = visibility.visible;
	}

	public object Convertback (object value, Type targettype, object parameter, System.Globalization.CultureInfo culture) { C7/>return (bool) value? Visibility.Visible:Visibility.Collapsed
	}
}



Code: http://download.csdn.net/detail/qing2005/4385971

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.