Trouble binding command in grid menu item.

Posted by Pete on Stack Overflow See other posts from Stack Overflow or by Pete
Published on 2011-01-13T16:50:10Z Indexed on 2011/01/13 16:53 UTC
Read the original article Hit count: 312

Filed under:
|
|
|

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>

© Stack Overflow or respective owner

Related posts about .NET

Related posts about wpf