Question

I try to bind the IsEnabled property in a context menuitem. The context menu is placed in a Devexpress GridControl.View.

I assume that the problem is in the DataContext. The barbutton item is searching for the property HasPermissionDelete in the DataContext of the grid. But this isn't the datacontext of the viewmodel. My property HasPermissionDelete is on the ViewModel.

I'm using caliburn.micro.

Can you help me for fixing this binding?

My xaml code:

<dxg:GridControl.View>
    <dxg:TableView x:Name="gridTable" AllowSorting="True" NavigationStyle="Row" MultiSelectMode="Row" AutoWidth="True" AllowEditing="False" AllowFilterEditor="True" AllowDrop="True" AllowGrouping="True" ShowGroupPanel="False" AllowMoveColumnToDropArea="True" SelectedRowsSource="{Binding SelectedUsers}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="RowDoubleClick">
                <cal:ActionMessage MethodName="Edit" />
            </i:EventTrigger>

            <i:EventTrigger EventName="Loaded">
                <cal:ActionMessage MethodName="InitializePrintableSource" >
                    <cal:Parameter Value="{Binding ElementName=gridTable}"></cal:Parameter>
                </cal:ActionMessage>
            </i:EventTrigger>
        </i:Interaction.Triggers>

        <dxg:TableView.RowCellMenuCustomizations>
            <dxb:BarButtonItem x:Name="btnDelete" Content="{lex:LocText Translations:Delete, Assembly=Prosa.Common.Modules.UserManagement}" IsEnabled="{Binding Path=HasPermissionDelete}"/>
        </dxg:TableView.RowCellMenuCustomizations>
    </dxg:TableView>
</dxg:GridControl.View>
Was it helpful?

Solution

Even if you use Caliburn Micro you can still fall back to standard XAML binding. I will make the assumption that your DevExpress DataGrid is inside a simple Grid, which is the root element for your Page or UserControl. So, you will have something like this structure:

<Grid x:Name="root">

      <dxg:GridControl>
       ...
       (your GridControl.View and everything else here)
       ...
      </dxg:GridControl>
</Grid>

So, to avoid using the GridControl's DataContext in your button, you can bind to the DataContext of your root grid, which in this case would be the corresponding ViewModel. Bind your button like this:

<dxb:BarButtonItem x:Name="btnDelete" Content="{lex:LocText Translations:Delete, Assembly=Prosa.Common.Modules.UserManagement}" 
IsEnabled="{Binding ElementName=root, Path=DataContext.HasPermissionDelete}"/>

You can always get to the datacontext you want in this way, by setting the Name of the element ("root" in this example) and then referring to it in the binding. You can also bind to elements with no name, but the binding expression is much more complicated (you have to use RelativeSource and select the element type).

Hope this solves your problem!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top