Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF键下突出显示+;datagrid中选定的最后一行_C#_Wpf_Select_Datagrid_Highlight - Fatal编程技术网

C#WPF键下突出显示+;datagrid中选定的最后一行

C#WPF键下突出显示+;datagrid中选定的最后一行,c#,wpf,select,datagrid,highlight,C#,Wpf,Select,Datagrid,Highlight,我正在开发一个应用程序,在这个应用程序中,用户可以向datagrid添加产品,并提供产品名称和价格等简单信息 例如,我想按键盘上的F4键,我想关注datagrid中的最后一项,这意味着选择它并突出显示该项 所以,伙计们,我怎么能做到这一点呢?我尝试了一些解决方案,比如将选定的索引设置到我的datagrid和smth,但都不起作用 谢谢各位, 干杯您可以使用InputBinding识别按下的F4键 <Window.InputBindings> <KeyBinding Ke

我正在开发一个应用程序,在这个应用程序中,用户可以向datagrid添加产品,并提供产品名称和价格等简单信息 例如,我想按键盘上的F4键,我想关注datagrid中的最后一项,这意味着选择它并突出显示该项

所以,伙计们,我怎么能做到这一点呢?我尝试了一些解决方案,比如将选定的索引设置到我的datagrid和smth,但都不起作用

谢谢各位,
干杯

您可以使用InputBinding识别按下的F4键

<Window.InputBindings>
    <KeyBinding Key="F4"
                Command="{Binding SelectLastItemCommand}" />
</Window.InputBindings>


您可以在此处查看如何选择项目:

您可以使用InputBinding来识别按下的F4键

<Window.InputBindings>
    <KeyBinding Key="F4"
                Command="{Binding SelectLastItemCommand}" />
</Window.InputBindings>


您可以在此处查看如何选择项目:

您的问题出在哪里?处理按钮事件或突出显示行?似乎是后者,请看以下内容:
你的问题出在哪里?处理按钮事件或突出显示行?似乎是后者,请看以下内容:
以编程方式突出显示
数据网格中的行或单元格
比仅设置
SelectedIndex
SelectedItem
属性要复杂一些

但是,通过访问
DataGrid
控件的可视用户界面元素并调用特定
DataGridCell
对象上的
UIElement.focus()
方法,可以选择并聚焦代码中的一行,并获得与使用鼠标时相同的行为,如以下博文所述

如何在WPF中以编程方式选择和聚焦数据网格中的行或单元格:

以下是一个例子:

public partial class MainWindow : Window
{
    public MainWindow
    {
        InitializeComponent();
        this.PreviewKeyDown += (s, e) => 
        {
            if(e.Key == Key.F4)
                SelectRowByIndex(dataGridProducts, dataGridProducts.Items.Count - 1);
        };

        //populate DataGrid etc...
    }

    private static void SelectRowByIndex(DataGrid dataGrid, int rowIndex)
    {
        if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
            throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow.");

        if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
            throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));

        dataGrid.SelectedItems.Clear();
        object item = dataGrid.Items[rowIndex];
        dataGrid.SelectedItem = item;

        DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
        if (row == null)
        {
            /* bring the data item (Product object) into view
             * in case it has been virtualized away */
            dataGrid.ScrollIntoView(item);
            row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
        }
        if (row != null)
        {
            DataGridCell cell = GetCell(dataGrid, row, 0);
            if (cell != null)
                cell.Focus();
        }
    }

    private static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
    {
        if (rowContainer != null)
        {
            System.Windows.Controls.Primitives.DataGridCellsPresenter presenter 
                = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
            if (presenter == null)
            {
                /* if the row has been virtualized away, call its ApplyTemplate() method 
                 * to build its visual tree in order for the DataGridCellsPresenter
                 * and the DataGridCells to be created */
                rowContainer.ApplyTemplate();
                presenter = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
            }
            if (presenter != null)
            {
                DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                if (cell == null)
                {
                    /* bring the column into view
                     * in case it has been virtualized away */
                    dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
                    cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                }
                return cell;
            }
        }
        return null;
    }

    private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is T)
                return (T)child;
            else
            {
                T childOfChild = FindVisualChild<T>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
    }
}
公共部分类主窗口:窗口
{
公共主窗口
{
初始化组件();
this.PreviewKeyDown+=(s,e)=>
{
如果(e.Key==Key.F4)
选择RowByIndex(dataGridProducts、dataGridProducts.Items.Count-1);
};
//填充数据网格等。。。
}
私有静态void SelectRowByIndex(DataGrid DataGrid,int rowIndex)
{
如果(!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
抛出新ArgumentException(“DataGrid的SelectionUnit必须设置为FullRow。”);
if(rowIndex<0 | | rowIndex>(dataGrid.Items.Count-1))
抛出新ArgumentException(string.Format(“{0}是无效的行索引。”,rowIndex));
dataGrid.SelectedItems.Clear();
对象项=dataGrid.Items[rowIndex];
dataGrid.SelectedItem=项目;
DataGridRow行=dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex)作为DataGridRow;
if(行==null)
{
/*查看数据项(产品对象)
*以防它被虚拟化了*/
dataGrid.ScrollingToView(项目);
行=dataGrid.ItemContainerGenerator.ContainerFromIndex(行索引)作为DataGridRow;
}
如果(行!=null)
{
DataGridCell=GetCell(dataGrid,行,0);
如果(单元格!=null)
cell.Focus();
}
}
私有静态DataGridCell GetCell(DataGrid DataGrid,DataGridRow rowContainer,int列)
{
if(rowContainer!=null)
{
System.Windows.Controls.Primitives.DataGridCellsPresenter演示器
=FindVisualChild(rowContainer);
如果(演示者==null)
{
/*如果行已被虚拟化,请调用其ApplyTemplate()方法
*为DataGridCellsPresenter构建其可视化树
*以及要创建的DataGridCell*/
rowContainer.ApplyTemplate();
演示者=FindVisualChild(rowContainer);
}
如果(演示者!=null)
{
DataGridCell=presenter.ItemContainerGenerator.ContainerFromIndex(列)作为DataGridCell;
if(单元格==null)
{
/*将该列显示在视图中
*以防它被虚拟化了*/
ScrollIntoView(rowContainer,dataGrid.Columns[column]);
单元格=presenter.ItemContainerGenerator.ContainerFromIndex(列)作为DataGridCell;
}
返回单元;
}
}
返回null;
}
私有静态T FindVisualChild(DependencyObject obj),其中T:DependencyObject
{
for(int i=0;i
以编程方式突出显示
数据网格中的行或单元格
比仅设置
SelectedIndex
SelectedItem
属性要复杂一些

但是,通过访问
DataGrid
控件的可视用户界面元素并调用特定
DataGridCell
对象上的
UIElement.focus()
方法,可以选择并聚焦代码中的一行,并获得与使用鼠标时相同的行为,如以下博文所述

如何在WPF中以编程方式选择和聚焦数据网格中的行或单元格:

以下是一个例子:

public partial class MainWindow : Window
{
    public MainWindow
    {
        InitializeComponent();
        this.PreviewKeyDown += (s, e) => 
        {
            if(e.Key == Key.F4)
                SelectRowByIndex(dataGridProducts, dataGridProducts.Items.Count - 1);
        };

        //populate DataGrid etc...
    }

    private static void SelectRowByIndex(DataGrid dataGrid, int rowIndex)
    {
        if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
            throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow.");

        if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
            throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));

        dataGrid.SelectedItems.Clear();
        object item = dataGrid.Items[rowIndex];
        dataGrid.SelectedItem = item;

        DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
        if (row == null)
        {
            /* bring the data item (Product object) into view
             * in case it has been virtualized away */
            dataGrid.ScrollIntoView(item);
            row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
        }
        if (row != null)
        {
            DataGridCell cell = GetCell(dataGrid, row, 0);
            if (cell != null)
                cell.Focus();
        }
    }

    private static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
    {
        if (rowContainer != null)
        {
            System.Windows.Controls.Primitives.DataGridCellsPresenter presenter 
                = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
            if (presenter == null)
            {
                /* if the row has been virtualized away, call its ApplyTemplate() method 
                 * to build its visual tree in order for the DataGridCellsPresenter
                 * and the DataGridCells to be created */
                rowContainer.ApplyTemplate();
                presenter = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
            }
            if (presenter != null)
            {
                DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                if (cell == null)
                {
                    /* bring the column into view
                     * in case it has been virtualized away */
                    dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
                    cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                }
                return cell;
            }
        }
        return null;
    }

    private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is T)
                return (T)child;
            else
            {
                T childOfChild = FindVisualChild<T>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
    }
}
公共部分类主窗口:窗口
{
公共主窗口
{
初始化组件();
this.PreviewKeyDown+=(s,e)=>
{
如果(e.Key==Key.F4)
选择RowByIndex(dataGridProducts,