我已经看到了几个类似的问题,请不要迅速将其忽略。这里的情况似乎有所不同,我看不出为什么会错。我的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中检查“输出”窗口,则可以看到以下说明:
System.Windows.Data错误:40:BindingExpression路径错误:在“集合的当前项”“ OCLMEditorModelView”(HashCode = 43686667)上找不到属性。BindingExpression:Path = /; 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”(HashCode = 472027)上找不到属性。BindingExpression:Path = /; DataItem ='RelativeSource'(HashCode = 472027); 目标元素是'MouseBinding'(HashCode = 36454430); 目标属性是“ CommandParameter”(类型“ 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}}"
除此之外,您还可以绑定到SelectedItem
via RelativeSource
,它应该是等效的。
CommandParameter="{Binding Path=SelectedItem,
RelativeSource={RelativeSource AncestorType=DataGrid}}"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句