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