我在表中添加了一条记录,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 中不起作用。