我在表SQLServer with WPF-application
的表中添加了一條記錄並且刷新了DataGrid顯示了一條新記錄。例如,我添加了具有name "Peter, last name "Pen"
的用戶,並且此記錄在DataGrid的末尾添加了。如何將重點放在這個記錄上並突出顯示?換句話說,how to move focus and highlight by name or surname?
專注或突出顯示WPF 2010中Datagrid的必要行
視圖模型有這樣的代碼:
<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?
}
請,作爲對我的好意!給予2010年WPF的例子作爲Visual C#代碼。如果你想設置focus on last added row
那就試試這個代碼不會在2010年WPF
爲了您的信息,您可以在您的案例中使用第二個示例。感謝 –
1.我是否必須使用DataGrid在同一表單上創建ListView並將ListView綁定到DataFrid的同一個適配器? 2.爲什麼要創建ListView因爲我想突出顯示DataGrid中的一行?也許你有一個與樣本的鏈接? – StepUp
這兩個例子都不同,使用不同的方法,而我沒有關於列表視圖的任何鏈接。我建議你,因爲我在我的專業的東西實施它但如果你想相關的例子,那麼你可以看到[這裏](http://social.msdn.microsoft.com/forums/en-US/wpf/thread/98d8423c-9719 -4291-94e2-c5bf3d80cd46 /) –