0
votes

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.

1
Don't use Convert.ChangeType just write (T)parameter. - H.B.
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.
@H.B. Updated question (bottom). - Andrew Truckle
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.
@H.B. Of course! The grid is already bound to the current item. No parameter needed. Silly me. Thank you. - Andrew Truckle

1 Answers

2
votes

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}}"