开发者

Trouble binding command in grid menu item

开发者 https://www.devze.com 2023-02-04 09:50 出处:网络
I have a grid that\'s inside a usercontrol derived class called MediatedUserControl. I\'m adding a context menu to let the user delete an item, but I\'ve been unable to figure out how to bind the comm

I have a grid that's inside a usercontrol derived class called MediatedUserControl. I'm adding a context menu to let the user delete an item, but I've been unable to figure out how to bind the command to my command property. I'm using MVVM and my viewmodel implements a public ICommand property called DeleteSelectedItemCommand.

However, when the view is displayed, I get the following message in the output window:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType=开发者_开发知识库'BRO.View.MediatedUserControl', AncestorLevel='1''. BindingExpression:Path=DataContext.DeleteSelectedItemCommand; DataItem=null; target element is 'BarButtonItem' (HashCode=6860584); target property is 'Command' (type 'ICommand')

I feel like I generally have a good handle on bindings like this and can't figure out what it is I'm missing here. Thanks for any help you can provide.

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


I solved it and I guess it has to do with the fact that it's a popup menu and therefore not in the window hierarchy. To that end, DevExpress provides a way to back:

Command="{Binding Path=(dxg:GridPopupMenu.GridMenuInfo).View.DataContext.DeleteSelectedItemCommand, RelativeSource={RelativeSource Self}}"
0

精彩评论

暂无评论...
验证码 换一张
取 消