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

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

我已经看到几个与我类似的问题,所以请不要很快忽略它。这里的情况似乎有所不同,我不明白为什么会出错。我的 DataGrid 对按键和鼠标点击有一些绑定:

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

感谢 Whosebug 和现有用户的贡献,我找到了方法。非常感谢。

此问题与 MouseBinding CommandParameter 有关。该程序执行正常,没有任何警告。我可以双击 DataGrid 中的任何行,它会按设计运行。

但是如果我在 Visual Studio 2015 中查看 Output window 我可以看到这条评论:

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')

为什么这么说?我使用 / 是因为 ItemSource 是一个 CollectionViewSource 对象,我知道 / 会调用当前选定的项目。但是在我实际双击一行之前,这个命令不应该被触发。

想知道如何才能阻止它出现在我的输出中 window。

如果我尝试根据答案更改绑定,我会收到此异常:

更新:

这里是当前XAML:

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

但现在我在输出中注意到这一点 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')

它似乎可以工作(我可以双击一行,它就可以完成我想要的)。那么我可以停止这个输出警告吗?

更新:

所以这是当前 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>

我尝试做的事情没有改变——当用户双击一个 DataGrid 行(它是 cvs 的一部分)时,它会调用一个基于该行的命令。

在输入绑定的上下文中,您的 DataContext 不应更改,因此我希望正确的绑定是:

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

除此之外,您还可以通过 RelativeSource 绑定到 SelectedItem,这应该是等效的。

CommandParameter="{Binding Path=SelectedItem,
                           RelativeSource={RelativeSource AncestorType=DataGrid}}"