Silver
Silver

Reputation: 453

Disable ContextMenu\right click on a Gridview row for a particular condition

I am new to wpf. I have a grid in wpf project

 <GridView x:Name="Train_View_Grid">
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Status}" Header="Status"/>
                    </GridView>

which has a context menu (right_Click) which appears on right click.

I have to disable this right click for a row whose status is finished.(one of the column is status whose value can be finished or running) But if the status is running we have to show right click menu.

  private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {

            hideContextMenu();
        }

private void hideContextMenu()
{  
                if (Train_Details_View.SelectedItems.Count > 0)
                {
                    DataRowView drv = Train_Details_View.SelectedItems[0] as DataRowView;

                    String journey_status = drv.Row["Status"].ToString();
                    if (journey_status.Equals("Finished"))
                    {
                        ContextMenu.Visibility = Visibility.Hidden;

                    }
                    }

    }

and i called it on the grid of context menu as

<Grid x:Name="Train_Info_Pnl" Grid.Column="2" Margin="0,0,10,0" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" PreviewMouseRightButtonDown="PEGrid_PreviewMouseRightButtonDown" >

Is my code above correct and where should i call this? If my code is wrong how can i correct it..

Upvotes: 0

Views: 1692

Answers (1)

franssu
franssu

Reputation: 2430

You cant set a Trigger on ContextMenuService.IsEnabled. Here is a pure xaml working sample :

<Grid>
    <ListView x:Name="LV">
        <ListView.ContextMenu>
            <ContextMenu>
                <Label Content="Your menu item..."/>
            </ContextMenu>
        </ListView.ContextMenu>

        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path=IsChecked}" 
                                Header="IsFinished"/>
            </GridView>
        </ListView.View>

        <ListView.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedItem.IsChecked, 
                                                   ElementName=LV}" Value="True">
                        <Setter Property="ContextMenuService.IsEnabled" 
                                Value="False"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Style>

        <CheckBox IsChecked="False"/>
        <CheckBox IsChecked="True"/>
    </ListView>
</Grid>

Upvotes: 3

Related Questions