【问题标题】:Select Row on RowClick not on Control Click在 RowClick 上选择行而不是在 Control Click 上
【发布时间】:2019-04-02 10:59:42
【问题描述】:

当我创建 DataGrid 时,默认情况下,要选择一行,我需要单击单元格内的控件。

我想在行中单击的任何位置选择该行。

有没有办法做到这一点?

<DataGrid AutoGenerateColumns="False"
      CanUserAddRows="False" 
      IsReadOnly="True"
      Style="{StaticResource DataGridStyle}"
      HorizontalContentAlignment ="Center"
      VerticalContentAlignment ="Center"
      VerticalScrollBarVisibility="Auto"
      SelectionMode="Single"
      ItemsSource="{Binding Items, Mode=OneWay}"
      SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<DataGrid.Columns>
    <DataGridTextColumn Header="Name"
                        Binding="{Binding Name}"
                        Width="4*" />
    <DataGridTextColumn Header="Description"
                        Binding="{Binding Description}"
                        Width="4*" />
</DataGrid.Columns>

【问题讨论】:

    标签: c# .net wpf datagrid


    【解决方案1】:

    如果你想选择行,即使你不会点击特定的单元格,你应该为 DataGridRow 添加带有 EventSetter 的 ItemContainerStyle,如下所示:

    <Grid>
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Customers}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <DataGridTextColumn Header="Name" Binding="{Binding Surname}" />
                <DataGridTextColumn Header="Name" Binding="{Binding Age}" />
            </DataGrid.Columns>
            <DataGrid.ItemContainerStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <EventSetter Event="MouseDown" Handler="DataGridRow_MouseDown" />
                </Style>
            </DataGrid.ItemContainerStyle>
        </DataGrid>
    </Grid>
    

    在代码隐藏中,您可能会从发送方获取 datarowgrid,遍历可视化树以获得 datagrid 本身(例如,如果您不想通过 x:name 调用它),然后只需设置 SelectedItem作为dataGridrow.Item

        private void DataGridRow_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var dataGridRow = (DataGridRow)sender;
            if (dataGridRow != null)
            {
                var dataGridRowParent = FindParent<DataGrid>(dataGridRow);
                if (dataGridRowParent != null)
                {
                    dataGridRowParent.SelectedItem = dataGridRow.Item;
                }
            }
        }
    
        public static T FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            //get parent item
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);
    
            //we've reached the end of the tree
            if (parentObject == null) return null;
    
            //check if the parent matches the type we're looking for
            T parent = parentObject as T;
            if (parent != null)
                return parent;
            else
                return FindParent<T>(parentObject);
        }
    

    【讨论】:

    • 谢谢,这确实回答了这个问题,但我喜欢有一个干净的 .xaml.cs。所以我决定删除 DataGridTextColumn 并将其替换为带有 TextBox 的 DataGridTemplate。通过更改大小、边距和对齐属性,我可以在 .xaml.cs 中无需代码来管理“错觉”或 RowSelection
    • 您好,不客气,先生。我想知道通过自定义行为或称为迭代的方式为事件设置器设置命令,但更改这些基本属性实际上是一种更快的解决方案。问候。
    猜你喜欢
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    相关资源
    最近更新 更多