防止在Xceed DataGrid上重新排序

时间:2014-06-11 06:16:32

标签: wpf wpftoolkit

我正在尝试取消xceed datagrid(社区编辑)网格中某些列的重新排序/拖放功能(http://wpftoolkit.codeplex.com/wikipage?title=DataGrid

到目前为止,我所做的是收听PreviewMouseLeftButtonUp / Down事件。然后我可以在Down set processed = true并且用户无法对列做任何事情(没有排序或任何事情)或者在Up上,检查IsBeingDragged是否为真。但是,如果我设置e.Handled,那么该列将保持拖动模式。我想要做的是取消整个拖动并将列放回原来的位置,或者(尽可能)尽可能靠近放置的列。

任何人都可以指导我吗?

private void ColumnManagerCell_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    // Check that the Original Source is a ColumnManagerCell
   if (sender.GetType() == typeof(Xceed.Wpf.DataGrid.ColumnManagerCell))
   {
       ColumnManagerCell col = sender as ColumnManagerCell;
       if (col.IsBeingDragged)
       {
            // user attempted to move the column to a new location, or to the GroupByControl area
            e.Handled = true;
       }
   }
}

1 个答案:

答案 0 :(得分:4)

来自Xceed DataGrid的文档:

http://doc.xceedsoft.com/products/XceedWpfDataGrid/#Grouping_Data.html

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
  xmlns:d="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
  xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase">
  <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                Source="{Binding Source={x:Static Application.Current},
                                                  Path=Orders}">
      <xcdg:DataGridCollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="ShipCountry" Direction="Ascending"/>
        <scm:SortDescription PropertyName="ShipCity" Direction="Ascending"/>
      </xcdg:DataGridCollectionViewSource.SortDescriptions>
      <xcdg:DataGridCollectionViewSource.GroupDescriptions>
        <xcdg:DataGridGroupDescription PropertyName="ShipCountry"/>
        <xcdg:DataGridGroupDescription PropertyName="ShipCity"/>
      </xcdg:DataGridCollectionViewSource.GroupDescriptions>
   </xcdg:DataGridCollectionViewSource>
 </Grid.Resources>
 <xcdg:DataGridControl x:Name="OrdersGrid"
                    ItemsSource="{Binding Source={StaticResource cvs_orders}}">      
   <xcdg:DataGridControl.Columns>
     <xcdg:Column FieldName="ShipCountry" VisiblePosition="0"/>
     <xcdg:Column FieldName="ShipCity" VisiblePosition="1"/>
   </xcdg:DataGridControl.Columns>
   <xcdg:DataGridControl.View>
     <xcdg:TableView FixedColumnCount="2" UseDefaultHeadersFooters="False">
       <xcdg:TableView.FixedHeaders>
         <DataTemplate>
           <xcdg:GroupByControl AllowSort="False" AllowGroupingModification="False"/>
         </DataTemplate>
         <DataTemplate>
          <xcdg:ColumnManagerRow AllowSort="False" AllowColumnReorder="False"/>
        </DataTemplate>
      </xcdg:TableView.FixedHeaders>
    </xcdg:TableView>
  </xcdg:DataGridControl.View>
</xcdg:DataGridControl>
</Grid>