访问silverlight数据网格的特定行(未选中)

时间:2012-07-18 21:37:41

标签: silverlight silverlight-5.0

我有一个用户可以添加记录的数据网格。 如果某个项目已经存在,我想滚动到相关行并突出显示它。

我找不到如何枚举datagrid中的行,我知道如何在observable集合中找到该项,但找不到如何查找相关行。

另外,欢迎使用重点提示,我还没有完成动画。

1 个答案:

答案 0 :(得分:1)

我创建了一个示例应用程序来检查datagrid中是否已经存在新添加的项目。如果存在,则选择它并滚动到查看该行。 我没有写过数据网格中不存在新添加项目的情况。

<my:DataGrid x:Name="PersonsDataGrid" AutoGenerateColumns="False" Loaded="PersonsDataGrid_Loaded" LoadingRow="PersonsDataGrid_LoadingRow">
  <my:DataGrid.Columns>
    <my:DataGridTextColumn Binding="{Binding FirstName}"  Header="First Name"/>
    <my:DataGridTextColumn Binding="{Binding LastName}" Header="Last Name"/>
  </my:DataGrid.Columns>

</my:DataGrid>  

namespace DataGridDemo  
{  
 public partial class MainPage : UserControl  
  {  

 private int selectedIndex = 0;
 private DataGridRow selectedRow = null;
    public MainPage()  
    {  
  InitializeComponent();  
  Persons = new List<Person>();

  Persons.Add(new Person(){FirstName = "Tony1", LastName = "Smith"});
  Persons.Add(new Person() { FirstName = "Jennifer1", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony2", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer2", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony3", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer3", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony4", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer4", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony5", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer5", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony6", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer6", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony7", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer7", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony8", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer8", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony9", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer9", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony10", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer10", LastName = "Suze" });

  Persons.Add(new Person() { FirstName = "Tony11", LastName = "Smith" });
  Persons.Add(new Person() { FirstName = "Jennifer11", LastName = "Suze" });

  PersonsDataGrid.ItemsSource = Persons;
}


private void PersonsDataGrid_Loaded(object sender, RoutedEventArgs e)
{
  Person newPerson = new Person() { FirstName = "Tony10", LastName = "Smith" };

  Person p = (from item in Persons
              where item.FirstName == newPerson.FirstName && item.LastName==newPerson.LastName
              select item).FirstOrDefault();


  if(p!=null)
  {
    selectedIndex = Persons.IndexOf(p);

    if (selectedIndex >= 0)
    {
      //PersonsDataGrid.SelectedIndex = selectedIndex ; - sometimes if selecteditem doesnot work
      PersonsDataGrid.SelectedItem = p;
      PersonsDataGrid.Dispatcher.BeginInvoke(() =>
                                               {
                                                 PersonsDataGrid.ScrollIntoView(p,null);
                                               });
    }
  }

}

 private void PersonsDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
  DataGridRow row = e.Row as DataGridRow;

  if (row.GetIndex() == selectedIndex)
  {
    selectedRow = row;
  }

}


public List<Person> Persons { get; set; }
}

public class Person  
{

public string FirstName { get; set; }

public string LastName { get; set; }


} 

}