How to disable relocation / re-creation of ListView header in WPF?

The ListView WPF control allows you to reorder columns by dragging and dropping. Is there any way to disable it?

I hope some WPF guru helps me. :)

+4
source share
2 answers
<ListView.View> <GridView AllowsColumnReorder="False"> ......headers here........ </GridView> </ListView.View> 

try it

+23
source

I am using a ListView WPF with N number of columns, where the 1st column should behave like a column of fields and it should remain 1st all the time

How can I disable re-ordering only for the 1st column and save other columns with reorderability?

I can disable the drag of the 1st column using the IsHitTestVisible property, which will disable the mouse inputs, but I noticed that the user can drag the second column (for example) and place it right in front of the first column, and this will change the 1st column with the second column ?

I figured out how to do this:

1) first subscribe to the event:

 GridView gridView = this.dbListView.View as GridView; gridView.Columns.CollectionChanged += new NotifyCollectionChangedEventHandler(Columns_CollectionChanged); 

2) Event handler:

  /// <summary> /// This event is executed when the header of the list view is changed - /// we need to keep the first element in it position all the time, so whenever user drags any columns and drops /// it right before the 1st column, we return it to it original location /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Columns_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { GridViewColumnCollection collection = (GridViewColumnCollection)sender; if (e.Action == NotifyCollectionChangedAction.Move) //re-order event { if (e.NewStartingIndex == 0) //if any of the columns were dragged rigth before the 1st column { this.Dispatcher.BeginInvoke((Action)delegate { GridView gridView = this.dbListView.View as GridView; //removing the event to ensure the handler will not be called in an infinite loop gridView.Columns.CollectionChanged -= new NotifyCollectionChangedEventHandler(Columns_CollectionChanged); //reverse the re-order move (ie rolling back this even) collection.Move(e.NewStartingIndex, e.OldStartingIndex); //re-setup the event to ensure the handler will be called second time gridView.Columns.CollectionChanged += new NotifyCollectionChangedEventHandler(Columns_CollectionChanged); }); } } } 
+4
source

All Articles