Home Forums WPF controls Xceed DataGrid for WPF Right click not selecting row

Tagged: 

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • lohikansun
    Participant
    Post count: 5
    #43548 |

    Hello,

    I current have a context menu opening up when a DataRow is right clicked, but I would like for the clicked row to also become selected. I have an event handler for the DataGrid_PreviewMouseRightButtonDown event (I also tried the non-preview event) that sets the SelectedItem and CurrentItem to the correct DataRowView, but for some reason this is not reflected in the datagrid. The SelectedItem is bound to a property in my view model (I’m using MVVM) so I know that it is being set properly.

    Please let me know if you have a solution to this issue.

    Code Behind Event

    private void DataGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
            {
                
                DependencyObject dep = (DependencyObject)e.OriginalSource;
                // iteratively traverse the visual tree
                while ((dep != null) && !(dep is Xceed.Wpf.DataGrid.DataRow))
                {
                    dep = VisualTreeHelper.GetParent(dep);
                }
    
                if (dep == null)
                    return;
    
                if (dep is Xceed.Wpf.DataGrid.DataRow)
                {
                    Xceed.Wpf.DataGrid.DataRow dataRow = dep as Xceed.Wpf.DataGrid.DataRow;
                    
                    GridControlViewModel viewModel = this.DataContext as GridControlViewModel;
                    DataTable table = viewModel.dataTable.Clone();
                    ////table.Rows.Add(dataRow);
                    //var a = table.DefaultView[0];
                    CellCollection Cells = dataRow.Cells;
                    List<object> a = new List<object>();
                    object[] items = new object[7];
                    items[5] = Cells[0].Content;
                    items[3] = Cells[1].Content;
                    items[0] = Cells[2].Content;
                    items[1] = Cells[3].Content;
                    items[2] = Cells[4].Content;
                    items[4] = Cells[5].Content;
                    items[6] = Cells[6].Content;
    
                    table.Rows.Add(items);
                    var view = table.DefaultView[0];
                    viewModel.SelectedItem = view;
                    
                    
                    DataGrid.CurrentItem = DataGrid.SelectedItem;
                    try
                    {
    
                        if (viewModel.MenuItemList.Count == 0)
                            Menu.Visibility = Visibility.Collapsed;
                        
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetType() != typeof(FormatException))
                            MessageBox.Show(ex.Message);
                    }
                    
                }
                
            }

    Datagrid in XAML

    <xcdg:DataGridControl x:Name="DataGrid"   VerticalAlignment="Stretch"   
                     ItemsSource="{Binding Source={StaticResource table_collection}}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                                  SelectedIndex="{Binding SelectedIndex}"  ItemContainerStyle="{StaticResource DataStyle}"
                                  PreviewMouseRightButtonDown="DataGrid_PreviewMouseRightButtonDown" PropertyChanged="DataGrid_PropertyChanged" SelectionMode="Single" ToolTipOpening="DataGrid_ToolTipOpening" PreviewMouseMove="DataGrid_PreviewMouseMove" 
                                 HorizontalContentAlignment="Left" PreviewMouseDown="DataGrid_PreviewMouseDown">
                <xcdg:DataGridControl.ContextMenu>
                    <ContextMenu x:Name="Menu" ItemsSource="{Binding MenuItemList, UpdateSourceTrigger=PropertyChanged}">
                    </ContextMenu>
                </xcdg:DataGridControl.ContextMenu>
    Fawzi [Xceed]
    Member
    Post count: 722

    Hi,

    What you could do to get a Row selected when you right click, is to subsribe to the DataCell’s PreviewMouseRightButtonDown event. Within your handler, you should modify the grid’s SelectedIndex property to select the item you are looking for. A simple code snippet which can be used to achieve this could be:

    XAML:

    <Style TargetType=”{x:Type xcdg:DataCell}”>
    <EventSetter Event=”PreviewMouseRightButtonDown” Handler=”previewMouseRightButtonDown ” />
    </Style>

    C#:

    void previewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
    var dataCell = sender as DataCell;
    if (dataCell == null)
    return;
    var parentRow = dataCell.ParentRow;
    var dataGrid = OrdersGrid;
    var dataItem = dataGrid.GetItemFromContainer(parentRow);
    var itemIndex = ((CollectionView)dataGrid.ItemsSource).IndexOf(dataItem);
    OrdersGrid.SelectedIndex = itemIndex;

    e.Handled = true;
    }

    lohikansun
    Participant
    Post count: 5

    That works great, thank you.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.