我有两个datagrid'
(主/细节)。我DispatcherTimer
每 5 秒刷新一次窗口。当我在我的主网格上选择任何行时,所选行上的焦点仅保留那 5 秒,之后选择焦点移动到最上面的行。
如何在 XAML 中克服这个问题?
编辑
public partial class MyWindow : Window
{
ProdEntities _prodEntities = new ProdEntities();
public MyWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
RebindData();
SetTimer();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void SetTimer()
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();
}
protected void dispatcherTimer_Tick(object sender, EventArgs e)
{
RebindData();
}
private void RebindData()
{
CollectionViewSource serversViewSource = (CollectionViewSource)(FindResource("serversViewSource"));
IQueryable<Server> serversQuery = this.GetServersQuery(_prodEntities );
serversViewSource.Source = serversQuery.ToList();
//serversViewSource.View.Refresh();
}
private IQueryable<Server> GetServersQuery(ProdEntities _prodEntities)
{
var serversQuery = _prodEntities.Servers.Where(c => c.Components.Any());
return serversQuery;
}
private void SaveChanges_Click(object sender, RoutedEventArgs e)
{
_prodEntities.SaveChanges();
}
}
XAML
<DataGrid AutoGenerateColumns="False" Foreground="DarkBlue" DataContext="StaticResource serversComponentsViewSource}"
ItemsSource="{Binding}" Name="componentsDataGrid">