C# 在行上选择行单击不在控件上单击

C# 在行上选择行单击不在控件上单击,c#,.net,wpf,datagrid,C#,.net,Wpf,Datagrid,默认情况下,当我创建一个DataGrid来选择一行时,我需要单击单元格内的控件 我想在行中单击的任何位置选择该行 有办法吗 <DataGrid AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" Style="{StaticResource DataGridStyle}" HorizontalContentAlignment ="Center"

默认情况下,当我创建一个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>


如果要选择行,即使不单击特定单元格,也应为DataGridRow添加ItemContainerStyle with EventSetter,如下所示:

<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);
    }
private void DataGridRow\u MouseDown(对象发送方,System.Windows.Input.MouseButtonEventArgs e)
{
var dataGridRow=(dataGridRow)发送方;
if(dataGridRow!=null)
{
var dataGridRowParent=FindParent(dataGridRow);
如果(dataGridRowParent!=null)
{
dataGridRowParent.SelectedItem=dataGridRow.Item;
}
}
}
公共静态T FindParent(DependencyObject子对象),其中T:DependencyObject
{
//获取父项
DependencyObject parentObject=VisualTreeHelper.GetParent(子级);
//我们已经到了树的尽头
if(parentObject==null)返回null;
//检查父项是否与我们要查找的类型匹配
T parent=parentObject作为T;
如果(父项!=null)
返回父母;
其他的
返回FindParent(parentObject);
}

谢谢,这确实回答了问题,但我希望有一个干净的.xaml.cs。所以我决定删除DataGridTextColumn,并用一个内置文本框的DataGridTemplate替换它。通过更改大小、边距和对齐属性,我可以在.xaml.csHi中管理“幻觉”或行选择,不需要任何代码,先生。我想知道如何通过自定义行为或称为iteractions的东西为eventsetter设置命令,但是更改这些基本属性实际上是一个更快的解决方案。当做