WPF ListView控件允许通过拖放重新排序列。 有没有办法禁用它?
我希望一些WPF大师可以帮助我。 :)
答案 0 :(得分:23)
<ListView.View>
<GridView AllowsColumnReorder="False">
......headers here........
</GridView>
</ListView.View>
试试这个
答案 1 :(得分:5)
我正在使用带有N个列的WPF listView,其中第1列必须像边距列一样,并且它应始终保持为第1列
如何仅禁用第1列的重新订购,并将其他列“重新排序”?
我可以使用IsHitTestVisible属性禁用第一列的拖放,这将禁用鼠标输入,但我注意到用户可以拖动第二列(例如)并将其放在第一列之前,这将交换第一列和第二列?
我想出了如何做到这一点:
1)首先订阅活动:
GridView gridView = this.dbListView.View as GridView;
gridView.Columns.CollectionChanged += new NotifyCollectionChangedEventHandler(Columns_CollectionChanged);
2)事件处理程序是:
/// <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);
});
}
}
}