4

I created an attached property to set commands using inputbinding on a datagridRow or datagricell in the style of the element.

Here im trying to add inputBinding on thing that are dynamically created such as datagridrows/cells, this is why im trying to add the inputbinding in the style.

Unfortunately the style dont allow me to use the InputBindings property of the datagridrows/cells in xaml and it need to be set in the code this is why i need the attached property.

The attached property propertychangedcallback methode is trigger on each row/cell creation and the methode recive the object on witch the property is set and the value of the property. The value is a input binding collection. In this collection the items are present but property of those item are not bind to their value

In the xaml you can see that I create only one InputBinding for all the rows/cells but I need to copy this Inputbinding on every row because they need their own commandParameter otherwise the commandParameter would be the same for every row/cell

When I remove the InputBindingsCollection and use a single InputBinding it works but i can only set on inputbinding and i want to have a mousebinding and a keybinding so I need to use the InputBindingsCollection.

When I use the InputBindingsCollection the inputBindings of the collection are not bind properly

In the InputBindingsPropertyChangedCallBack method the inpuBind.Commandand inpuBind.CommandParameter are null 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)
        {
           //*****Here is my problem inpuBind.Command and inpuBind.CommandParameter are not bind to their value and are null
            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)
    {
        //throw new NotImplementedException();
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        //throw new NotImplementedException();
    }
}


Model1 :

public class Model1
{
    public string Nom { get; set; }
    public int Age { get; set; }

    public Model1(string n, int a)
    {
        Nom = n;
        Age = a;
    }
}
3
  • I guess you have the same problem as this guy (see the marked answer) - social.msdn.microsoft.com/Forums/vstudio/en-US/…
    – lena
    Jan 31, 2014 at 7:38
  • OMG, you're doing so much unnecessary work here... you don't need to use Attached Properties to set a MouseBinding or KeyBinding for an ICommand. Just use RelayCommands instead. Then you can just data bind to the Commands from the view model: <KeyBinding Gesture="CTRL+S" Command="{Binding Save, Mode=OneWay}" />
    – Sheridan
    Jan 31, 2014 at 13:48
  • Here im trying too add inputBinding on thing that are dynamiclly created such as datagridrows/cells, this is why i m trying to add the inputbinding in the style, The style dont allow to use the InputBindings property of the datagridrows/cells in xaml and it need to be set in the code and can't use what you said<br> the attached property propertychangedcallback methode is trigger on each row/cell creation. <br> In the xaml I create only one InputBinding for all the rows/cells but I need to copy the Inputbinding on every row because they need their own commandParameter but the binding dont work Jan 31, 2014 at 22:54

1 Answer 1

0

If i'am talking right the attached property or command Class must be static

so you have to write public static Class AttachedCommand{}

1
  • i tried what you said but it didnt change anything, the binding still not working Jan 31, 2014 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.