简体   繁体   中英

Hide specific context menu in datagrid upon selected row wpf mvvm

I got one issue on datagrid using WPF Mvvm. I set context menu on datagrid. here is my code.

 <DataGrid.ContextMenu>
                <ContextMenu IsEnabled="{Binding IsEnableCaseRefNo}" 
                             DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"  >                                                       
                    <MenuItem Header=" - View Case" >
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding ContextCommand}" CommandParameter="VCD"></i:InvokeCommandAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </MenuItem>
                    <MenuItem Header=" - Cheque" Visibility="{Binding SyncColumnVisibility, Converter={StaticResource visibilityConverter}}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding ContextCommand}" CommandParameter="BMK"></i:InvokeCommandAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </MenuItem>
                    <MenuItem Header=" - Cash" Visibility="{Binding SyncColumnVisibility, Converter={StaticResource visibilityConverter}}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding ContextCommand}" CommandParameter="UNBMK"></i:InvokeCommandAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </MenuItem>
               </ContextMenu>
            </DataGrid.ContextMenu>

I can show and hide context menu using visibility property. but now i want to enable/disable "-Cheque" context menu upon user selection. how can I disable 'Cheque' context menu when there are 100 over dollars in donate columns(that already shown in datagrid).

Here is my datagrid :

 <DataGrid Name="dgv" Background="WhiteSmoke" AutoGenerateColumns="False" CanUserAddRows="False" SelectedItem="{Binding SelectedItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                      CanUserDeleteRows="False" Grid.Row="2" ItemsSource="{Binding LstcaseHearingModel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"                              
                      Grid.Column="2" HorizontalAlignment="Stretch" >

Thanks for any help. frog

  1. Use SelectedCellsChanged event to get desired item.

      private void Dgrd_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { SomeEntity item = (SomeEntity) Dgrd.CurrentItem; if(item.Donate > 100) viewModel.SyncColumnVisibility = Visibility.Collapsed; else viewModel.SyncColumnVisibility = Visibility.Visible; } 

As you are binding SelectedItem property to SelectedItems property of your ViewModel. So you can check the condition in its setter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM