Wpf 单元格内容的隐藏部分,甚至整个隐藏列。@Metro Smurf:这是一种折衷。在我看来,如果你有一个合理的列数(不是太多),最好不要使用滚动条,但有时你被迫显示大量的列,在这种情况下,最好使用滚动条,以便所有的列都有合理的宽度,在这种情况下,您将不会有最后

Wpf 单元格内容的隐藏部分,甚至整个隐藏列。@Metro Smurf:这是一种折衷。在我看来,如果你有一个合理的列数(不是太多),最好不要使用滚动条,但有时你被迫显示大量的列,在这种情况下,最好使用滚动条,以便所有的列都有合理的宽度,在这种情况下,您将不会有最后,wpf,listview,datagrid,selection,Wpf,Listview,Datagrid,Selection,单元格内容的隐藏部分,甚至整个隐藏列。@Metro Smurf:这是一种折衷。在我看来,如果你有一个合理的列数(不是太多),最好不要使用滚动条,但有时你被迫显示大量的列,在这种情况下,最好使用滚动条,以便所有的列都有合理的宽度,在这种情况下,您将不会有最后一列后面出现的空白。将我的2美分添加到这个伟大的响应中:如果您在控件中使用InputBindings,它将不再工作,因为InputBindings需要焦点才能正常工作。要修复它,您需要手动将焦点设置在DataGrid上:var dg=sende


单元格内容的隐藏部分,甚至整个隐藏列。@Metro Smurf:这是一种折衷。在我看来,如果你有一个合理的列数(不是太多),最好不要使用滚动条,但有时你被迫显示大量的列,在这种情况下,最好使用滚动条,以便所有的列都有合理的宽度,在这种情况下,您将不会有最后一列后面出现的空白。将我的2美分添加到这个伟大的响应中:如果您在控件中使用InputBindings,它将不再工作,因为InputBindings需要焦点才能正常工作。要修复它,您需要手动将焦点设置在DataGrid上:var dg=sender为DataGrid;dg.Focus();快速而干净的解决方案,您可能需要添加
SomeGrid.UnselectAll()选择新行之前,避免多选。
 private void SomeGridMouseDown(object sender, MouseButtonEventArgs e)
    {
        var dependencyObject = (DependencyObject)e.OriginalSource;

        //get clicked row from Visual Tree
        while ((dependencyObject != null) && !(dependencyObject is DataGridRow))
        {
            dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
        }

        var row = dependencyObject as DataGridRow;
        if (row == null)
        {
            return;
        }

        row.IsSelected = true;
    }
<UserControl x:Class="WpfDemo.Views.Default"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:b="clr-namespace:WpfDemo.Behaviors"
         mc:Ignorable="d" 
         d:DesignHeight="300">

        <DataGrid b:DataGridBehavior.FullRowSelect="True">
              ...
        </DataGrid>             
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfDemo.Behaviors
{
    /// <summary>
    /// Extends <see cref="DataGrid"/> element functionality.
    /// </summary>
    public static class DataGridBehavior
    {
        #region - Dependency properties -

        /// <summary>
        /// Forces row selection on empty cell, full row select.
        /// </summary>
        public static readonly DependencyProperty FullRowSelectProperty = DependencyProperty.RegisterAttached("FullRowSelect",
            typeof(bool),
            typeof(DataGridBehavior),
            new UIPropertyMetadata(false, OnFullRowSelectChanged));

        #endregion

        #region - Public methods -

        /// <summary>
        /// Gets property value.
        /// </summary>
        /// <param name="grid">Frame.</param>
        /// <returns>True if row should be selected when clicked outside of the last cell, otherwise false.</returns>
        public static bool GetFullRowSelect(DataGrid grid)
        {
            return (bool)grid.GetValue(FullRowSelectProperty);
        }

        /// <summary>
        /// Sets property value.
        /// </summary>
        /// <param name="grid">Frame.</param>
        /// <param name="value">Value indicating whether row should be selected when clicked outside of the last cell.</param>
        public static void SetFullRowSelect(DataGrid grid, bool value)
        {
            grid.SetValue(FullRowSelectProperty, value);
        }

        #endregion

        #region - Private methods -

        /// <summary>
        /// Occurs when FullRowSelectProperty has changed.
        /// </summary>
        /// <param name="depObj">Dependency object.</param>
        /// <param name="e">Event arguments.</param>
        private static void OnFullRowSelectChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            DataGrid grid = depObj as DataGrid;
            if (grid == null)
                return;

            if (e.NewValue is bool == false)
            {
                grid.MouseDown -= OnMouseDown;

                return;
            }

            if ((bool)e.NewValue)
            {
                grid.SelectionMode = DataGridSelectionMode.Single;

                grid.MouseDown += OnMouseDown;
            }
        }

        private static void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            var dependencyObject = (DependencyObject)e.OriginalSource;

            while ((dependencyObject != null) && !(dependencyObject is DataGridRow))
            {
                dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
            }

            var row = dependencyObject as DataGridRow;
            if (row == null)
            {
                return;
            }

            row.IsSelected = true;
        }

        #endregion
    }
}
_dataGrid.MouseLeftButtonDown += (sender, args) =>
{
        //If click was on row border, reroute to latest cell of row
        if (!(args.OriginalSource is Border border) || !(border.Child is SelectiveScrollingGrid grid)) return;

        var cellsPresenter = grid.Children.OfType<DataGridCellsPresenter>().FirstOrDefault();
        if (cellsPresenter == null || cellsPresenter.Items.Count < 1) return;

        var lastCell = (DataGridCell)cellsPresenter.ItemContainerGenerator.ContainerFromIndex(cellsPresenter.Items.Count - 1);
        lastCell.RaiseEvent(args);
};