简体   繁体   English

WPF Datagrid文本框行已选择

[英]Wpf Datagrid textbox row selected

I have an problem with a datagrid where a column is a Textbox with a ContextMenu. 我有一个数据网格的问题,其中一列是带有ContextMenu的文本框。 But the problem is that then i right click on the textbox the row is not selected. 但是问题是,然后我右键单击文本框未选择该行。 Which means that it can set the values in the wrong place. 这意味着它可以将值设置在错误的位置。 In the picture below it's shown'en the issue. 在下面的图片中显示了问题所在。 I have right clicked the top row, but it's still the row below is selected, which means that if i select "Mixed Paint" it will like the picture be inserted below the intended row. 我右键单击了第一行,但是仍然选择了下面的行,这意味着如果我选择“混合油漆”,它将喜欢将图片插入到预期的行下方。 在此处输入图片说明

This is the code for the column: 这是该列的代码:

<DataGridTemplateColumn Header="{wpfTx:Translate Action}" IsReadOnly="false" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Action, Mode=TwoWay}" TextWrapping="Wrap" BorderThickness="0" BorderBrush="Transparent">
                                   <TextBox.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem ItemsSource="{Binding ActionMenu}">
                                                <MenuItem.Icon>
                                                    <controls:Icon IconKeyName="Config" Height="45" Width="45"/>
                                                </MenuItem.Icon>
                                                <MenuItem.Header >
                                                    <Label Content="Standard actions" VerticalContentAlignment="Center" FontSize="16" FontWeight="Bold"/>
                                                </MenuItem.Header>
                                                <MenuItem.ItemTemplate>
                                                    <DataTemplate>
                                                        <MenuItem Command="{Binding ActionMenuCommand}" CommandParameter="{Binding}">
                                                            <MenuItem.Header>
                                                                <Label Content="{Binding Description}" FontSize="16" FontWeight="Bold"/>
                                                            </MenuItem.Header>
                                                            <MenuItem.Icon>
                                                                <controls:Icon IconKeyName="Edited" Height="45" Width="45"/>
                                                            </MenuItem.Icon>
                                                        </MenuItem>
                                                    </DataTemplate>
                                                </MenuItem.ItemTemplate>
                                            </MenuItem>
                                        </ContextMenu>
                                    </TextBox.ContextMenu>
                                </TextBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

I found the solution to the problem: 我找到了解决问题的方法:

In the xaml code i added this: 在xaml代码中,我添加了以下内容:

  <DataGrid MouseRightButtonUp="UIElement_OnMouseRightButtonUp" Name="dataGrid1"></DataGrid>

And Code behind: 和后面的代码:

  private void UIElement_OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            var dep = (DependencyObject)e.OriginalSource;
            while ((dep != null) && !(dep is DataGridCell))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
            if (dep == null) return;

            if (dep is DataGridCell)
            {
                var cell = dep as DataGridCell;
                cell.Focus();

                while ((dep != null) && !(dep is DataGridRow))
                {
                    dep = VisualTreeHelper.GetParent(dep);
                }
                var row = dep as DataGridRow;
                dataGrid1.SelectedItem = row.DataContext;   
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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