2012-05-05 1 views
2

WPFデータグリッド項目にマウスをダブルクリックして追加することができますことは、当社のデータグリッドビューは、行マウスのダブルクリックのようなイベントがたくさんあるか、クリックして、余分な行...どのように私はWindowsで

しかし、WPFで、私は見つける傾けるアプリケーションを形成これらの出来事。私はデータグリッドマウスのダブルクリックイベントを使用して、いくつかのバグがこのように起こったいくつかの悪い方法でそれをやったことで

をデータグリッドを持っているどのように私は私のユーザーコントロールに行マウスのダブルクリックを追加することができます しかし、私はまた、row_loadイベントにデータグリッド項目にダブルクリックイベントを追加し、シンプルで標準的な方法

を知ってほしいが、私のプログラムが遅くなりそうですデータグリッドは、大きなソースを持っている場合

private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
    e.Row.MouseDoubleClick += new MouseButtonEventHandler(Row_MouseDoubleClick); 
} 

答えて

7

あなたが扱うことができるデータグリッド要素をダブルをクリックし、クリックされた行と列を見つけるために、イベントソースを見て:

private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    DependencyObject dep = (DependencyObject)e.OriginalSource; 

    // iteratively traverse the visual tree 
    while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader)) 
    { 
     dep = VisualTreeHelper.GetParent(dep); 
    } 

    if (dep == null) 
     return; 

    if (dep is DataGridColumnHeader) 
    { 
     DataGridColumnHeader columnHeader = dep as DataGridColumnHeader; 
     // do something 
    } 

    if (dep is DataGridCell) 
    { 
     DataGridCell cell = dep as DataGridCell; 
     // do something 
    } 
} 

私はthis blog post that I wroteにこれを詳細に説明します。

+0

おかげで私はシンプルで良い:))グレートのために働いていた使用クリックしたときに知ってほしいと ! –

0

コリンの答えは本当に良かったし、働きました...私もこのコードを使用しています。これは私にとって有益なことであり、他の人と分かち合いたいと思っています。

private void myGridView_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
     { 
      DependencyObject dep = (DependencyObject)e.OriginalSource; 

      // iteratively traverse the visual tree 
      while ((dep != null) && !(dep is DataGridRow) && !(dep is DataGridColumnHeader)) 
      { 
       dep = VisualTreeHelper.GetParent(dep); 
      } 

      if (dep == null) 
       return; 

      if (dep is DataGridRow) 
      { 
       DataGridRow row = dep as DataGridRow; 
       //here i can cast the row to that class i want 
      } 
     } 

私は、すべての行が、私はこの

関連する問題