【问题标题】:Wpf Datagrid textbox row selectedWpf Datagrid 文本框行被选中
【发布时间】:2014-04-24 13:19:35
【问题描述】:

我有一个数据网格的问题,其中列是带有 ContextMenu 的文本框。但问题是,然后我右键单击未选择行的文本框。这意味着它可以将值设置在错误的位置。在下图中,它显示了问题。我已经右键单击了第一行,但它仍然选择了下面的行,这意味着如果我选择“混合油漆”,它会喜欢将图片插入到预期行的下方。

这是专栏的代码:

<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>

【问题讨论】:

    标签: c# wpf xaml wpfdatagrid


    【解决方案1】:

    我找到了问题的解决方案:

    在 xaml 代码中我添加了这个:

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

    以及背后的代码:

      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;   
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多