Silverlight 4 race condition with DataGrid master details control

Posted by Simon_Weaver on Stack Overflow See other posts from Stack Overflow or by Simon_Weaver
Published on 2010-05-05T06:58:59Z Indexed on 2010/05/23 2:10 UTC
Read the original article Hit count: 419

Basically I want a DataGrid (master) and details (textbox), where DataGrid is disabled during edit of details (forcing people to save/cancel)... Here's what I have...

I have a DataGrid which serves as my master data.

<data:DataGrid IsEnabled="{Binding CanLoad,ElementName=dsReminders}" 
               ItemsSource="{Binding Data, ElementName=dsReminders}" >

Its data comes from a DomainDataSource:

 <riaControls:DomainDataSource Name="dsReminders" AutoLoad="True" ...

I have a bound Textbox which is the 'details' (very simple right now). There are buttons (Save/Cancel) which should be enabled when user tries to edit the text. Unfortunately Silverlight doesn't support UpdateSourceTrigger=PropertyChanged so I have to raise an event:

<TextBox Text="{Binding SelectedItem.AcknowledgedNote, Mode=TwoWay, UpdateSourceTrigger=Explicit, ElementName=gridReminders}" 
         TextChanged="txtAcknowledgedNote_TextChanged"/>

The event to handle this calls BindingExpression.UpdateSource to update the source immediately:

private void txtAcknowledgedNote_TextChanged(object sender, TextChangedEventArgs e)
    {
        BindingExpression be = txtAcknowledgedNote.GetBindingExpression(TextBox.TextProperty);
        be.UpdateSource();
    }

IN other words - typing in the textbox causes CanLoad of the DomainDataSource to become False (because we're editing). This in turn disables the DataGrid (IsEnabled is bound to it) and enables 'Cancel' and 'Save' buttons.

However I'm running up against a race condition if I move quickly through rows in the DataGrid (just clicking random rows). The TextChanged presumably is being called on the textbox and confusing the DomainDataSource which then thinks there's been a change.

So how should I disable the DataGrid while editing without having the race condition?

One obvious solution would be to use KeyDown events to trigger the call to UpdateSource but I always hate having to do that.

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about silverlight-4.0