我有一个名为MediatedUserControl的usercontrol派生类内的网格。我正在添加一个上下文菜单让用户删除一个项目,但我一直无法弄清楚如何将命令绑定到我的命令属性。我正在使用MVVM,我的viewmodel实现了一个名为DeleteSelectedItemCommand的公共ICommand属性。
但是,当显示视图时,我在输出窗口中收到以下消息:
System.Windows.Data错误:4:无法找到与引用'RelativeSource FindAncestor绑定的源,AncestorType ='BRO.View.MediatedUserControl',AncestorLevel ='1''。 BindingExpression:路径= DataContext.DeleteSelectedItemCommand;的DataItem = NULL; target元素是'BarButtonItem'(HashCode = 6860584); target属性是'Command'(类型'ICommand')
我觉得我通常对这样的绑定有很好的处理能力并且无法弄清楚我在这里缺少什么。感谢您提供的任何帮助。
<dxg:GridControl HorizontalAlignment="Left" Margin="12,88,0,0" x:Name="gridControl1" VerticalAlignment="Top" Height="500" Width="517" DataSource="{Binding ItemList}" BorderBrush="{StaticResource {x:Static SystemColors.ActiveBorderBrushKey}}" ShowBorder="True" Background="{StaticResource {x:Static SystemColors.ControlLightBrushKey}}" UseLayoutRounding="False" DataContext="{Binding}">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Code" Header="Code" Width="107" />
<dxg:GridColumn FieldName="Name" Header="Item" Width="173" />
<dxg:GridColumn FieldName="PricePerItem" Header="Unit Price" Width="70">
<dxg:GridColumn.EditSettings>
<dxe:TextEditSettings DisplayFormat="N2" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Quantity" Header="Qty" Width="50" AllowEditing="True" />
<dxg:GridColumn FieldName="TotalPrice" Header="Total Price" Width="90">
<dxg:GridColumn.EditSettings>
<dxe:TextEditSettings DisplayFormat="N2" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView ShowIndicator="False" ShowGroupPanel="False" MultiSelectMode="Row" AllowColumnFiltering="False" AllowBestFit="False" AllowFilterEditor="False" AllowEditing="False" AllowGrouping="False" AllowSorting="False" AllowResizing="False" AllowMoving="False" AllowMoveColumnToDropArea="False" AllowDateTimeGroupIntervalMenu="False" >
<dxg:TableView.RowCellMenuCustomizations>
<dxb:BarButtonItem Name="deleteRowItem" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=view:MediatedUserControl, AncestorLevel=1}, Path=DataContext.DeleteSelectedItemCommand}">
</dxb:BarButtonItem>
</dxg:TableView.RowCellMenuCustomizations>
</dxg:TableView>
</dxg:GridControl.View>
答案 0 :(得分:3)
我解决了它,我猜它与弹出菜单有关,因此不在窗口层次结构中。为此,DevExpress提供了一种支持方式:
Command="{Binding Path=(dxg:GridPopupMenu.GridMenuInfo).View.DataContext.DeleteSelectedItemCommand, RelativeSource={RelativeSource Self}}"