2013-05-14 73 views
3

我有一個DataGrid樣式模板,我希望添加雙擊行爲。綁定應該是正確的,但我似乎無法得到xaml編譯/工作。鼠標雙擊DataGrid行

添加到IDictionary的所有對象都必須有一個關鍵屬性 或與之相關的一些其他類型的密鑰。

下面的代碼有什麼問題?每維克多的評論

<Style TargetType="{x:Type DataGridRow}"> 
    <EventSetter Event="MouseDoubleClick" Handler="{Binding Connect}"/> 

更新(給出確切的同樣的錯誤):

<Style x:Key="dataGridRowStyle" TargetType="{x:Type DataGridRow}"> 
    <EventSetter Event="PreviewMouseDoubleClick" Handler="{Binding Connect}"/> 
+0

那對於一個ListView,和我的代碼是在附近上相同的,但我仍然得到這個錯誤... – Chris 2013-05-14 10:40:30

+0

確定...這個問題很可能是在其他地方。爲該樣式提供x:Key。一切都會安好的。 – 2013-05-14 10:47:55

+0

謝謝,但是這給了我完全相同的錯誤... – Chris 2013-05-14 10:51:19

回答

2

您可以應用在數據網格行以下行爲,並按照實施使用。

雙擊行爲

public class DoubleClickBehavior 
    { 
     #region DoubleClick 

     public static DependencyProperty OnDoubleClickProperty = DependencyProperty.RegisterAttached(
      "OnDoubleClick", 
      typeof(ICommand), 
      typeof(DoubleClickBehavior), 
      new UIPropertyMetadata(DoubleClickBehavior.OnDoubleClick)); 

     public static void SetOnDoubleClick(DependencyObject target, ICommand value) 
     { 
      target.SetValue(OnDoubleClickProperty, value); 
     } 

     private static void OnDoubleClick(DependencyObject target, DependencyPropertyChangedEventArgs e) 
     { 
      var element = target as Control; 
      if (element == null) 
      { 
       throw new InvalidOperationException("This behavior can be attached to a Control item only."); 
      } 

      if ((e.NewValue != null) && (e.OldValue == null)) 
      { 
       element.MouseDoubleClick += MouseDoubleClick; 
      } 
      else if ((e.NewValue == null) && (e.OldValue != null)) 
      { 
       element.MouseDoubleClick -= MouseDoubleClick; 
      } 
     } 

     private static void MouseDoubleClick(object sender, MouseButtonEventArgs e) 
     { 
      UIElement element = (UIElement)sender; 
      ICommand command = (ICommand)element.GetValue(OnDoubleClickProperty); 
      command.Execute(null); 
     } 

     #endregion DoubleClick 
    } 

使用

 <Style BasedOn="{StaticResource {x:Type DataGridRow}}" 
       TargetType="{x:Type DataGridRow}"> 
      <Setter Property="Helpers:DoubleClickBehavior.OnDoubleClick" Value="{Binding Path=DataContext.MyCommandInVM, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ViewLayer:MyUserControl}}}" /> 
     </Style> 
2

不知道,如果你要的MVVM路線,但我已經取得了使用Attached Command Behavior要連接的雙擊事件這個功能我的viewmodel命令(其中「命令」是我的attachedCommandBehavior程序集/類的引用):

<DataGrid.RowStyle> 
     <Style TargetType="{x:Type DataGridRow}"> 
      <Setter Property="command:CommandBehavior.Event" Value="MouseDoubleClick"/> 
      <Setter Property="command:CommandBehavior.Command" Value="{Binding SelectItemCmd}"/> 
      <Setter Property="command:CommandBehavior.CommandParameter" Value="{Binding }"/> 
     </Style> 
</DataGrid.RowStyle> 
6

我們可以使用數據網格化InputBindings實現的目標:

<DataGrid.InputBindings> 
    <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding SomeCommand}" /> 
</DataGrid.InputBindings> 
+0

工程像一個魅力 – EmilianoPe 2016-01-25 14:38:41