【问题标题】:Focus or Highlighting a necessary row of Datagrid in WPF 2010在 WPF 2010 中聚焦或突出显示必要的 Datagrid 行
【发布时间】:2012-10-06 13:01:00
【问题描述】:

我在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 中不起作用。

【问题讨论】:

    标签: wpf datagrid highlight


    【解决方案1】:

    对于 WPF,您必须在 Listview 中添加 gridview 控件,之后您可以轻松地在 Gridview 中选择和聚焦特定记录。 否则你必须使用 DataGrid 控件来处理这种东西。

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

    【讨论】:

    • 供您参考,您可以在您的案例中使用第二个示例。谢谢
    • 1.我是否必须在与 DataGrid 相同的表单上创建 ListView 并将 ListView 绑定到 DataFrid 的同一适配器? 2. 为什么要创建 ListView,因为我想在 DataGrid 中突出显示一行?也许您有示例链接?
    • 这两个例子使用不同的方法是不同的,而且我没有任何关于列表视图的链接。我建议你,因为我在我的专业资料中实现了它但是如果你想要相关的例子,那么你可以看到here
    • Hey StepUp,我发现一篇类似这样的数据网格的帖子,您可以通过Here 找到它,希望对您有所帮助。问候!
    • 我试过这段代码:)。这是行不通的。它会产生错误。
    【解决方案2】:

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

    【讨论】:

    • 谢谢,但它在 WPF 中不起作用,因为此代码用于 Visual C#。代码 Visual C# 在 WPF 中不起作用。
    • 是的,但是您可以在 WPF 中使用它进行选择。实际上在 WPF 中你必须使用 ListView 来做这样的事情。有关更多详细信息,请参阅我的另一个答案。
    • 如果你想用gridview那你可能得用listview否则不用用,直接用datagrid控件就可以了。
    猜你喜欢
    • 1970-01-01
    • 2011-10-01
    • 2023-03-29
    • 2013-06-11
    • 2011-05-27
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多