如何以编程方式在WPF DataGrid中选择行或单元格?

new*_*man 10 c# wpf select datagrid row

在WinForm DataGridView中,它会在初始化时自动选择第一行.当我试图关掉这个功能时,它让我抓狂.转移到WPF DataGrid,似乎微软决定关闭这个功能,这是我认为的好事.但是,我现在很难启用此功能.对于某些DataGrid,我希望在通过数据绑定填充网格后自动选择第一行.互联网上有一些建议,但我无法做到这一点.我希望在这里有更好的运气.

ASa*_*nch 10

设置IsSynchronizedWithCurrentItem = "true".

编辑:

为了解决您的评论,我假设您的DataGrid的SelectionUnit设置为"Cell",是吗?好的,我不确定这是否是最佳解决方案,但您可以做的一件事是处理DataGrid的Loaded事件并在代码隐藏中手动设置所选单元格.所以你会有这样的事情:

<DataGrid x:Name="dg" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True"
            SelectedCellsChanged="dg_SelectedCellsChanged" SelectionUnit="Cell"
            Loaded="dg_Loaded">
    ...
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

事件处理程序:

private void dg_Loaded(object sender, RoutedEventArgs e)
{
    if ((dg.Items.Count > 0) &&
        (dg.Columns.Count > 0))
    {
        //Select the first column of the first item.
        dg.CurrentCell = new DataGridCellInfo(dg.Items[0], dg.Columns[0]);
        dg.SelectedCells.Add(dg.CurrentCell);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,这仅在DataGrid.SelectionUnit设置为"Cell"时才有效.否则,我相信会抛出异常.

EDIT2:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button Click="Button_Click">Reset</Button>
        <DataGrid x:Name="dg" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True"
                SelectionUnit="Cell"
                DataContextChanged="dg_DataContextChanged"
                ItemsSource="{Binding Items}"
                Loaded="dg_Loaded">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding}"/>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.LoadItems();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.LoadItems();
        }

        private void LoadItems()
        {
            this.DataContext = new { Items = new List<string> { "Item1", "Item2", "Item3" } };
            this.SelectFirstItem();
        }

        private void dg_Loaded(object sender, RoutedEventArgs e)
        {
            SelectFirstItem();
        }

        void SelectFirstItem()
        {
            if ((dg.Items.Count > 0) &&
                (dg.Columns.Count > 0))
            {
                //Select the first column of the first item.
                dg.CurrentCell = new DataGridCellInfo(dg.Items[0], dg.Columns[0]);
                dg.SelectedCells.Add(dg.CurrentCell);
            }
        }

        private void dg_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.SelectFirstItem();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)