在 WPF 2010 中聚焦或突出显示必要的 Datagrid 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12914520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:47:48  来源:igfitidea点击:

Focus or Highlighting a necessary row of Datagrid in WPF 2010

wpfdatagridhighlight

提问by StepUp

I added a record in a table of SQLServer with WPF-applicationand refresh DataGridshown a new record. For instance, I add user which has name "Peter, last name "Pen"and this record adds at the end of DataGrid. How to move focus on this record and highlight on that? In other words, how to move focus and highlight by name or surname?

我在表中添加了一条记录SQLServer with WPF-application并刷新DataGrid显示了一条新记录。例如,我添加了用户,name "Peter, last name "Pen"并且此记录添加在 DataGrid 的末尾。如何将注意力转移到这条记录上并突出显示?换句话说,how to move focus and highlight by name or surname?

ViewModel has such code:

ViewModel 有这样的代码:

<Window x:Class="Students.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        Title="MainWindow" Height="996" Width="1191" xmlns:my="clr-namespace:Students" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen">    
    <Window.Resources>        
        <my: StudentDataSet x:Key="StudentDataSet" />
        <CollectionViewSource x:Key="StudentViewSource" Source="{Binding Path=S_DEP, Source={StaticResource StudentDataSet}}" />          
    </Window.Resources>

<Grid>
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="615" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource StudentViewSource}}" Margin="21,322,0,0" Name="StudentDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="1046">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="NameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" MinWidth="110" />
                <DataGridTextColumn x:Name="LastNameColumn" Binding="{Binding Path=LastName}" Header="LastName" Width="SizeToHeader" MinWidth="100"/>                
                <DataGridTextColumn x:Name="PhoneColumn" Binding="{Binding Path=PHONE}" Header="Phone Number" Width="SizeToHeader" MinWidth="105" />               
            </DataGrid.Columns>
        </DataGrid>
</Grid>

Model has such code:

模型有这样的代码:

UserBoard.StudentDataSet aRCHDOC_1DataSet = ((UserBoard.StudentDataSet)(this.FindResource("StudentDataSet")));            
            // Loading data in the table Student            UserBoard.StudentDataSetTableAdapters.StudentTableAdapter StudentDataSet_StudentTableAdapter = new UserBoard.StudentDataSetTableAdapters.StudentTableAdapter();
            StudentDataSet_StudentTableAdapter.Fill(StudentDataSet.Students);
            System.Windows.Data.CollectionViewSource StudentViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("StudentViewSource")));
            StudentViewSource.View.MoveCurrentToFirst();


            //Highlighting a necessary row
            string position = e.Someth_property;
            for (int i = 0; i < StudentDataGrid.Items.Count; i++)
            {
                //What do I should write here?   
            }

Please, as a kindness to me! Give examples for WPF 2010 as code of Visual C# does not work in WPF 2010.

请,作为对我的善意!给出 WPF 2010 的示例,因为 Visual C# 的代码在 WPF 2010 中不起作用。

回答by Bhavik Patel

If you want to set focus on last added rowthen try this code:

如果要设置,请focus on last added row尝试以下代码:

dataGridView.ClearSelection();
int RwIndex= dataGridView.Rows.Count - 1;

dataGridView.Rows[RwIndex].Selected = true;
dataGridView.Rows[RwIndex].Cells[0].Selected = true;

回答by Bhavik Patel

For WPF you have to add gridview control inside Listview, after that you can easily select and focus particular record in Gridview. Otherwise you must have to use DataGrid Control for this kind of stuff.

对于 WPF,您必须在里面添加 gridview 控件Listview,之后您可以轻松地在 Gridview 中选择和聚焦特定记录。否则,您必须使用 DataGrid Control 来处理此类事情。

For example (Listview)refer this code:

例如(Listview)参考这个代码:

myListView.SelectedItem = myListView.Items[index];
myListView.ScrollIntoView(myListView.Items[index]);
ListViewItem listViewItem = myListView.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
listViewItem.Focus(); 

For example (DataGrid):

例如(DataGrid)

int index = 11;
myDataGrid.SelectedItem = myDataGrid.Items[index];
myDataGrid.ScrollIntoView(myDataGrid.Items[index]);
DataGridRow dgrow =(DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.Items[index]);
dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));