Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh WPF DataGrid without losing cell focus

I have a WPF DataGrid with some data bound to LINQ to SQL entity classes. One column is a clock showing a given flight's airborn time, which is calculated using logic in Flight's partial class. I have a timer calling datagrid.Items.Refresh every 2 seconds to update the clock.

The refresh works fine, but now I'm adding keyboard shortcuts. Navigating through cells with the keyboard arrows works fine with the timer off, but with the Refresh timer enabled, the focused cell (actually the entire datagrid) loses focus.

I need to either somehow maintain focus (preferred) or disable the timer whenever the DataGrid is focused. I can't even seem to get the latter to work. I've tried:

if (!dataGrid.IsFocused)
    dataGrid.Items.Refresh();

and

if (!dataGrid.IsKeyboardFocused)
        dataGrid.Items.Refresh();

for the timer, but these properties return false even when the datagrid is focused.

Any ideas?

like image 546
Anders Avatar asked Mar 26 '11 14:03

Anders


1 Answers

The best way is not to use dataGrid.Items.Refresh() as update mechanism. It sounds like the underlying objects are already updated separately. If they implement INotifyPropertyChanged, you should try to set the binding mode for the colum as TwoWay Binding:

<DataGridTextColumn Binding="{Binding xyz, Mode=TwoWay}"/>
like image 125
Alex Maker Avatar answered Oct 15 '22 12:10

Alex Maker