I created an attached property for a set of commands using inputbinding in a datagridRow or datagricell in the element style.
Here I am trying to add an inputBinding to things that are dynamically created, such as datagridrows / cells, so im trying to add an input binding in style.
Unfortunately, the style does not allow me to use the InputBindings property for datagridrows / cells in xaml, and it needs to be set in the code, so I need a nested property.
The bound property of a propertychangedcallback method is a trigger for each row / cell creation, and the method returns an object when the property is set and the value of the property. The value is a collection of input bindings. Elements are present in this collection, but the property of this element is not tied to their value.
In xaml, you can see that I only create one InputBinding for all rows / cells, but I need to copy this Inputbinding into each row because they need their own commandParameter parameter, otherwise the commandParameter parameter will be the same for each row / cell
When I remove InputBindingsCollection and use one InputBinding, it works, but I can only set input binding, and I want to have mouse binding and key binding, so I need to use InputBindingsCollection.
When I use InputBindingsCollection, inputBindings of the collection are not bound properly
In the method InputBindingsPropertyChangedCallBack inpuBind.Commandand inpuBind.CommandParameterare zeros instead of the binding value
AttacheCommand
public class AttacheCommand
{
public static DependencyProperty InputBindingsProperty = DependencyProperty.RegisterAttached("InputBindings",
typeof(InputBindingCollection),
typeof(AttacheCommand),
new PropertyMetadata(InputBindingsPropertyChangedCallBack));
public static void SetInputBindings(UIElement element, InputBindingCollection value)
{
element.SetValue(InputBindingsProperty, value);
}
public static InputBindingCollection GetInputBindings(UIElement element)
{
return (InputBindingCollection)element.GetValue(InputBindingsProperty);
}
public static void InputBindingsPropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
InputBinding copy;
foreach (InputBinding inpuBind in e.NewValue as InputBindingCollection)
{
copy = new InputBinding(inpuBind.Command, inpuBind.Gesture);
copy.CommandParameter = inpuBind.CommandParameter;
element.InputBindings.Add(copy);
}
}
}
View:
<Window x:Class="UltimateCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UltimateCommand"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel x:Name="vm"/>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding listModel1}" IsReadOnly="True">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="local:AttacheCommand.InputBindings" >
<Setter.Value>
<InputBindingCollection>
<MouseBinding Command="{Binding ElementName=vm, Path=cmd1}" CommandParameter="{Binding}" Gesture="LeftClick"/>
</InputBindingCollection>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</Grid>
</Window>
ViewModel: (and command)
class ViewModel
{
public Model1[] listModel1
{
get
{
return new Model1[] { new Model1("nom1", 1), new Model1("nom2", 2) };
}
}
public Command1 cmd1
{
get
{
return new Command1();
}
}
}
public class Command1 : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
}
}
Model1:
public class Model1
{
public string Nom { get; set; }
public int Age { get; set; }
public Model1(string n, int a)
{
Nom = n;
Age = a;
}
}