0

I have seen several similar questions to mine so please don't quickly dismiss it. The scenario seems different here and I can't see why it would be wrong. My DataGrid has some bindings to keys and mouse clicks:

<DataGrid x:Name="gridStudents" ItemsSource="{Binding Source={StaticResource cvsStudentList}}"
    Margin="2"
    Height="250"
    SelectedItem="{Binding SelectedStudentItem, UpdateSourceTrigger=PropertyChanged}"
    AutoGenerateColumns="False" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" SelectionChanged="gridStudents_SelectionChanged">
    <DataGrid.InputBindings>
        <MouseBinding
            MouseAction="LeftDoubleClick"
            Command="{Binding EditStudentButtonClickCommand}"
            CommandParameter="{Binding /}" />
        <KeyBinding Key="Delete" Command="{Binding DeleteStudentButtonClickCommand}" />
    </DataGrid.InputBindings>

I found the methodology thanks to StackOverflow and existing user contributions. Much appreciated.

This issue relates to the MouseBinding CommandParameter. The program executes fine with no warnings. I can double-click any row in the DataGrid and it behaves as designed.

But if I check the Output window in Visual Studio 2015 I can see this remark:

System.Windows.Data Error: 40 : BindingExpression path error: '' property not found on 'current item of collection' ''OCLMEditorModelView' (HashCode=43686667)'. BindingExpression:Path=/; DataItem='OCLMEditorModelView' (HashCode=43686667); target element is 'MouseBinding' (HashCode=49684624); target property is 'CommandParameter' (type 'Object')

Why is it saying this? I used / because the ItemSource is a CollectionViewSource object and I understood that / invokes the currently selected item. But this command is not supposed to fire until I actually double-click a row anyway.

Curious as to how I can stop this appearing in my output window.

If I try to change the binding as per the answer I get this exception:

Exception error

Update:

Here is the current XAML:

<MouseBinding
    MouseAction="LeftDoubleClick"
    Command="{Binding EditStudentButtonClickCommand}"
    CommandParameter="{Binding /, Source={RelativeSource Self}}" />

But now I notice this in the output window:

System.Windows.Data Error: 40 : BindingExpression path error: '' property not found on 'current item of collection' ''RelativeSource' (HashCode=472027)'. BindingExpression:Path=/; DataItem='RelativeSource' (HashCode=472027); target element is 'MouseBinding' (HashCode=36454430); target property is 'CommandParameter' (type 'Object')

It seems to work (I can double-click a row and it does what I want). So can I stop this output warning?

Update:

So this is the current XAML:

<DataGrid x:Name="gridStudents" ItemsSource="{Binding StudentsView}"
    Margin="2"
    Height="250"
    SelectedItem="{Binding SelectedStudentItem, UpdateSourceTrigger=PropertyChanged}"
    AutoGenerateColumns="False" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" SelectionChanged="gridStudents_SelectionChanged">
    <DataGrid.InputBindings>
        <MouseBinding
            MouseAction="LeftDoubleClick"
            Command="{Binding EditStudentButtonClickCommand}"
            CommandParameter="{Binding /, Source={RelativeSource Self}}" />
        <KeyBinding Key="Delete" Command="{Binding DeleteStudentButtonClickCommand}" />
    </DataGrid.InputBindings>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="VerticalContentAlignment" Value="Center"/>
        </Style>
    </DataGrid.CellStyle>

What I am tryign to do has not changed - when user double-clicks a DataGrid row (which is boudn to a cvs) it invokes a command based on that row.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • Don't use `Convert.ChangeType` just write `(T)parameter`. – H.B. Jun 27 '16 at 16:49
  • The `RelativeSource` *extension* is only valid for the `RelativeSource` *property*, not `Source`. Also `RelativeSource Self` would refer to the `MouseBinding` which surely is not your collection, what are you even doing... – H.B. Jun 30 '16 at 12:32
  • @H.B. Updated question (bottom). – Andrew Truckle Jun 30 '16 at 12:46
  • 1
    Your binding is still broken and that was not a question, it was a statement about how you don't seem to know how to construct valid bindings. Also: If your command does what it is supposed to do without getting passed a command parameter (because the binding fails) then *stop passing a parameter in the first place*. – H.B. Jun 30 '16 at 17:15
  • @H.B. Of course! The grid is already bound to the current item. No parameter needed. Silly me. Thank you. – Andrew Truckle Jun 30 '16 at 17:28

1 Answers1

2

In the context of the input bindings your DataContext should not have changed, so i would expect the correct binding to be:

CommandParameter="{Binding Path=/, Source={StaticResource cvsStudentList}}"

Other than that you also can bind to the SelectedItem via RelativeSource, which should be equivalent.

CommandParameter="{Binding Path=SelectedItem,
                           RelativeSource={RelativeSource AncestorType=DataGrid}}"
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Thanks. If I try this first approach with the static resource I get an exception when double clicking. – Andrew Truckle Jun 27 '16 at 16:08
  • "an exception" is not very helpful. The question is whether the binding itself works and what exactly the command implementation expects to be passed as parameter. – H.B. Jun 27 '16 at 16:43
  • I can't do this. The parameter is the current **item** in the cvs source. I will added the exception I get to my question. All I know is that I have to just use / alone and it operates. But I get that silent exception. If I fiddle with bindings as described I get exception. Can you please show me in your answer the SelectedItem idea you mentioned? Thanks. – Andrew Truckle Jun 27 '16 at 16:44
  • Added example. Also, looking at the exception it may have only worked so far because it always passed nothing to the converter because the binding was broken. – H.B. Jun 27 '16 at 16:51
  • Thanks. Still raises error. I checked further. The error is because it is tryign to convert a "Student" object with a "Name" string value. That was the column I left double-clicked on. We don't need any such conversion. – Andrew Truckle Jun 27 '16 at 16:52
  • @AndrewTruckle: Expected as much. – H.B. Jun 27 '16 at 17:03
  • I can't seem to make it work, sorry, not even if I change my delegate. – Andrew Truckle Jun 27 '16 at 17:20
  • 1
    That is a problem completely unrelated to the binding which should work fine now. i.e. ask another question with more details on the command implementation if you want help with that. – H.B. Jun 27 '16 at 17:22
  • Sorted. :) Did what you said. And my DelegateCommand calls needed changign to DelegateCommand and not DelegateCommand. :) – Andrew Truckle Jun 27 '16 at 17:41