Search code examples
c#wpfxamlbindingcommandparameter

BindingExpression path error: '' property not found on 'current item of collection' ... BindingExpression:Path=/;


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.


Solution

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