1

Доброго времени суток. Пытаюсь сделать так что бы команда DoubleClick срабатывала только по DataGridRow. Использую свойство зависимости:

public static DependencyProperty DoubleClickCommandProperty = DependencyProperty.RegisterAttached(
                "DoubleClickCommand",
                typeof(ICommand),
                typeof(DataGridRowBehavior),
                new UIPropertyMetadata(DoubleClick_PropertyChanged));

public static void SetDoubleClickCommand(DependencyObject target, ICommand value)
{
      target.SetValue(DataGridRowBehavior.DoubleClickCommandProperty, value);
}

public static ICommand GetDoubleClickCommand(UIElement element)
{
      return (ICommand)element.GetValue(DoubleClickCommandProperty);
}

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

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

private static void OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
      DependencyObject obj = sender as DependencyObject;
      ICommand cmd = (ICommand)obj.GetValue(DoubleClickCommandProperty);
      if (cmd != null)
      {
           if (cmd.CanExecute(obj))
               cmd.Execute(obj);
      }
}

Во view у меня следующее:

.
.
.
<Window.Resources>
        <Style x:Key="DefaultRowStyle" TargetType="{x:Type DataGridRow}">
            <Setter Property="help:DataGridRowBehavior.DoubleClickCommand" Value="{Binding local:EditProduct}" />
        </Style>
</Window.Resources>
.
.
.
<DataGrid Grid.Row="1" x:Name="MyDG"
                          ItemsSource="{Binding Path=ProductCol}"
                          RowStyle="{StaticResource DefaultRowStyle}"
                          SelectedItem="{Binding Path=CurrentProduct}"
                          AutoGenerateColumns="True"
                          CanUserAddRows="False"
                          SelectionUnit="FullRow"
                          IsReadOnly="True">

</DataGrid>

А в ViewModel выглядет вот так:

class MainWindowViewModel : ViewModelBase
{
    private Product currentProduct;

    public Product CurrentProduct
    {
        get 
        {
           return currentProduct; 
        }
        set 
        {
           currentProduct = value;
           Debug.WriteLine(currentProduct.Id);
           OnPropertyChanged("CurrentProduct");
        }
    }

    ObservableCollection<Product> product;
    public ObservableCollection<Product> ProductCol
    {
        get
        {
            if (product == null)
                product = new ObservableCollection<Product>();
            return product;
        }
    }

    public MainWindowViewModel()
    {
        ProductCol.Add( new Product() {Id = 1, Code = 5236});
        ProductCol.Add(new Product() { Id = 2, Code = 2857 });
        ProductCol.Add(new Product() { Id = 3, Code = 7851 });
    }

    RelayCommand editProduct;
    public ICommand EditProduct
    {
        get
        {
            if (editProduct == null)
                editProduct = new RelayCommand(ExecuteEditCurrentProductCommand,
                        CanExecuteEditCurrentProductCommand);

            return editProduct;
        }
    }

    private bool CanExecuteEditCurrentProductCommand(object obj)
    {
        return CurrentProduct == null ? false : true;
    }

    private void ExecuteEditCurrentProductCommand(object obj)
    {
        var oEW = new ObjectEditorWindow();
        var vm = new ObjectEditorViewModel<Product>();
        vm.SelectedObject = CurrentProduct;
        oEW.DataContext = vm;
        oEW.ShowDialog();
    }

    protected override void OnDispose()
    {
        this.ProductCol.Clear();
    }
}

Обратил внимание на вот такую ошибку...

System.Windows.Data Error: 40 : BindingExpression path error: 'EditProduct' property not found on 'object' ''Product' (HashCode=53239939)'. BindingExpression:Path=EditProduct; DataItem='Product' (HashCode=53239939); target element is 'DataGridRow' (Name=''); target property is 'DoubleClickCommand' (type 'ICommand')

Как я понимаю компилятор ищет свойство EditProduct в классе Product. Если это так, почему так происходит?Помогите, пожалуйста, разобраться.

1 ответ 1

1

Всё потому, что DataContext у DataGrid - коллекция Product. Естественно, что WPF ищет команду в нём, ведь вы так и написали. Вам надо переопределить DoubleClick на самом DataGrid, используя выбранный Product как аргумент команды.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.