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

时间:2012-10-16 12:16:55

标签: wpf datagrid highlight

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

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>

模型有这样的代码:

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?   
            }

请对我好心!为WPF 2010提供示例,因为Visual C#的代码在WPF 2010中不起作用。

2 个答案:

答案 0 :(得分:1)

如果您想设置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;

答案 1 :(得分:1)

对于WPF,您必须在Listview中添加gridview控件,之后您可以轻松选择并关注Gridview中的特定记录。 否则,您必须使用DataGrid Control来处理这类内容。

例如(Listview)请参阅此代码:

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

例如(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));