[英]Binding not working correctly in silverlight
我在包含自定义复选框列的Silverlight项目中使用了DataGrid。 我已经将其Command
属性绑定到ViewModel类的属性。 现在,问题在于我想通过我为其编写了以下代码的命令参数发送DataGrid的“选定项”:
<sdk:DataGrid AutoGenerateColumns="False" Margin="10,0,10,0" Name="dataGridOrders" ItemsSource="{Binding OrderList}" Height="190">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Select">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox>
<is:Interaction.Triggers>
<is:EventTrigger EventName="Checked">
<is:InvokeCommandAction Command="{Binding Source={StaticResource ExecutionTraderHomePageVM},Path=OrderSelectedCommand,Mode=TwoWay}"
CommandParameter="{Binding ElementName=dataGridOrders,Path=SelectedItem}" />
</is:EventTrigger>
<is:EventTrigger EventName="Unchecked">
<is:InvokeCommandAction Command="{Binding Source={StaticResource ExecutionTraderHomePageVM},Path=OrderSelectedCommand,Mode=TwoWay}"
CommandParameter="{Binding ElementName=dataGridOrders,Path=SelectedItem}" />
</is:EventTrigger>
</is:Interaction.Triggers>
</CheckBox>
但是我总是在命令的Execute
方法的参数中获取null
。 我尝试了DataGrid的其他属性,例如Width
, ActualHeight
等,但没有用。 我在这里想念什么?
这里的问题是名称范围界定之一。 简而言之, DataTemplate
内部的XAML具有自己的“名称范围”,并且看不到其外部的任何名称。 特别是,它将无法找到位于DataTemplate
外部的父DataGrid
。
我不确定为什么您要侦听已检查和未检查的事件并根据它们触发命令。 尚不清楚您要实现的目标。 相反,我会用一个双向绑定到绑定IsChecked
的财产CheckBox
的bool
属性在你(行级)视图模型类,并调用此命令的功能bool
属性的设置方法。 您可以通过双向绑定将视图模型属性绑定到DataGrid
的SelectedItem
属性来控制所选项目。
我认为你应该像这样使用EventTrigger
<is:EventTrigger EventName="Checked">
<is:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CheckBoxUncheckedCommand}" CommandParameter="{Binding}"/>
</is:EventTrigger>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.