【问题标题】:BindingExpression path error: '' property not found on 'current item of collection' ... BindingExpression:Path=/;BindingExpression 路径错误:在“当前集合项”上找不到“属性”... BindingExpression:Path=/;
【发布时间】:2016-06-27 15:48:21
【问题描述】:

我已经看到了几个与我类似的问题,所以请不要很快忽略它。这里的情况似乎不同,我不明白为什么会出错。我的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>

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

此问题与MouseBinding CommandParameter 有关。该程序执行良好,没有任何警告。我可以双击DataGrid 中的任何一行,它的行为与设计相同。

但如果我检查 Visual Studio 2015 中的 Output 窗口,我可以看到以下备注:

System.Windows.Data 错误:40:BindingExpression 路径错误:在“当前集合项”“OCLMEditorModelView”(HashCode=43686667)上找不到“属性”。绑定表达式:路径=/; DataItem='OCLMEditorModelView' (HashCode=43686667);目标元素是“MouseBinding”(HashCode=49684624);目标属性是'CommandParameter'(类型'Object')

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

很好奇如何阻止它出现在我的输出窗口中。

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

更新:

这是当前的 XAML:

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

但现在我在输出窗口中注意到了这一点:

System.Windows.Data 错误:40:BindingExpression 路径错误:'' 在“当前收藏项”“RelativeSource”上找不到属性 (哈希码=472027)'。绑定表达式:路径=/; DataItem='RelativeSource' (HashCode=472027);目标元素是 '鼠标绑定' (HashCode=36454430);目标属性是 'CommandParameter'(类型'对象')

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

更新:

这就是当前的 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)时,它会调用基于该行的命令。

【问题讨论】:

  • 不要使用Convert.ChangeType,只写(T)parameter
  • RelativeSource 扩展名仅对RelativeSource 属性有效,对Source无效。另外RelativeSource Self 会引用MouseBinding,这肯定不是你的收藏,你到底在做什么......
  • @H.B.更新的问题(底部)。
  • 您的绑定仍然损坏,这不是一个问题,这是关于您似乎不知道如何构建有效绑定的声明。另外:如果您的命令在没有传递命令参数的情况下完成了它应该做的事情(因为绑定失败),那么首先停止传递参数
  • @H.B.当然!网格已经绑定到当前项目。不需要参数。傻我。谢谢。

标签: c# wpf xaml binding commandparameter


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢。如果我尝试使用静态资源的第一种方法,双击时会出现异常。
  • “异常”不是很有帮助。问题是绑定本身是否有效,以及命令实现究竟期望作为参数传递什么。
  • 我做不到。参数是 cvs 源中的当前 item。我将添加我遇到的问题的例外情况。我所知道的是,我只需要单独使用 / 它就可以运行。但我得到了那个无声的例外。如果我按照描述摆弄绑定,我会得到异常。您能否在回答中向我展示您提到的 SelectedItem 想法?谢谢。
  • 添加示例。另外,看看这个异常,它可能只工作到现在,因为它总是没有向转换器传递任何东西,因为绑定被破坏了。
  • 这是一个与绑定完全无关的问题,现在应该可以正常工作了。即,如果您需要帮助,请询问有关命令实现的更多详细信息的另一个问题。
猜你喜欢
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
相关资源
最近更新 更多