2013-11-14 18 views
5
私は4.0

DataGridのWPF仮想化とコマンドCanExecute

は、私は、DataGridに問題があるフレームワーク.NETとWPFアプリケーションに取り組んでいます

public ICommand MoveUpOrderPipeCommand 
{ 
    get 
    { 
     if (_moveUpOrderPipeCommand == null) 
     { 
       _moveUpOrderPipeCommand = new Command<OrderPipeListUIModel>(OnMoveUpOrderPipe, CanMoveUpOrderPipe); 
     } 
       return _moveUpOrderPipeCommand; 
     } 
} 

private bool CanMoveUpOrderPipe(OrderPipeListUIModel orderPipe) 
{ 
    if (OrderPipes == null || !OrderPipes.Any() || OrderPipes.First() == orderPipe) 
      return false; 
    return true; 
} 

とがあります:すべての行は、2つのコマンドを得ましたMoveDownのために同じコマンド(行が最後のものでない場合は、チェックを実行することができます)

データグリッド:

<DataGrid Grid.Row="1" IsReadOnly="True" ItemsSource="{Binding OrderPipes}" SelectionMode="Extended"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Diam. (mm)" Binding="{Binding Diameter}" Width="120"> </DataGridTextColumn> 
     <DataGridTextColumn Header="Lg. (m)" Binding="{Binding Length}" Width="120"></DataGridTextColumn> 
     <DataGridTextColumn Header="Ep. (mm)" Binding="{Binding Thickness}" Width="120"></DataGridTextColumn> 
     <DataGridTextColumn Header="Ondulation" Binding="{Binding Ripple}" Width="120"></DataGridTextColumn> 
     <DataGridTemplateColumn> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.MoveUpOrderPipeCommand}" CommandParameter="{Binding}"> 
        </Button> 
       </StackPanel> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

EnableRowVirtualizationを使ってグリッドを真にすると、一番下の行(最初の行はもう表示されなくなります)をスクロールして上にスクロールすると、最初の行のボタン移動(normalyはできません。上に移動)は、DataGridをクリックするまで有効です。また、2番目または3番目のものが無効になっている必要があります。

私はfalseにEnableRowVirtualizationを設定した場合、私はこの問題を持っていない...

私はこの問題について話をインターネット上で一つの他の記事を見つけましたが、.NETフレームワークからのDataGridがありません: http://www.infragistics.com/community/forums/t/15189.aspx

どのように修正できますか?

は事前に

編集ありがとう:コマンドクラス

public class Command<T> : ICommand 
{ 
    private readonly Action<T> _execute; 
    private readonly Func<T, bool> _canExecute; 

    public Command(Action<T> execute) : this(execute, null) 
    { 
    } 

    public Command(Action<T> execute, Func<T, bool> canExecute) 
    { 
     if (execute == null) 
      throw new ArgumentNullException("execute", "Le délégué execute ne peut pas être nul"); 

     this._execute = execute; 
     this._canExecute = canExecute; 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add 
     { 
      CommandManager.RequerySuggested += value; 
     } 
     remove 
     { 
      CommandManager.RequerySuggested -= value; 
     } 
    } 

    public bool CanExecute(object parameter) 
    { 
     return (_canExecute == null) ? true : _canExecute((T)parameter); 
    } 

    public void Execute(object parameter) 
    { 
     _execute((T)parameter); 
    } 
} 
+0

CanExecuteは呼び出されていますか? –

+0

'Command'クラスのコードを投稿します。 –

+0

CanExecuteはスクロールで呼び出されません – Tan

答えて

4

を使用すると、マウスホイールをスクロールするときに問題があり、canExecuteは呼び出されません。

これを修正するためにAttachedPropertyを作成し、スタイルで使用することができます。

public static readonly DependencyProperty CommandRefreshOnScrollingProperty = DependencyProperty.RegisterAttached(
      "CommandRefreshOnScrolling", 
      typeof(bool), 
      typeof(DataGridProperties), 
      new FrameworkPropertyMetadata(false, OnCommandRefreshOnScrollingChanged)); 

private static void OnCommandRefreshOnScrollingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
     var dataGrid = d as DataGrid; 
     if (dataGrid == null) 
     { 
      return; 
     } 
     if ((bool)e.NewValue) 
     { 
     dataGrid.PreviewMouseWheel += DataGridPreviewMouseWheel; 
     } 
} 
private static void DataGridPreviewMouseWheel(object sender, MouseWheelEventArgs e) 
{ 
    CommandManager.InvalidateRequerySuggested(); 
} 

そして、あなたは、このようなスタイルでこのattachedPropertyを使用することができます。私はこの問題を得た理由

<Setter Property="views:DataGridProperties.CommandRefreshOnScrolling" Value="True"></Setter> 

おかげエランOtzapは私を見るために!

関連する問題