The WPF ListView control allows to re-order of columns by drag and drop. Is there any way to disable it?
I hope some WPF guru can help me. :)
<ListView.View>
<GridView AllowsColumnReorder="False">
......headers here........
</GridView>
</ListView.View>
Try this
I'm using WPF listView with N number of columns where the 1st column must behave like a margin column and it should remain the 1st all the times
How Can I disable the re-order for 1st column only, and keep other columns "reorder-able"?
I can disable the drag-drop of the 1st column using IsHitTestVisible property which will disable the mouse inputs, but I noticed that user can drag the 2nd column (for example) and drop it right before the 1st column and this will swap the 1st column with the 2nd column?
I figured out how to do it:
1) first subscribe to the event:
GridView gridView = this.dbListView.View as GridView;
gridView.Columns.CollectionChanged += new NotifyCollectionChangedEventHandler(Columns_CollectionChanged);
2) Event handler is:
/// <summary>
/// This event is executed when the header of the list view is changed -
/// we need to keep the first element in it's position all the time, so whenever user drags any columns and drops
/// it right before the 1st column, we return it to it's 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 (i.e. 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);
});
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With