Validation after update from code RRS feed

  • Question

  • I have a DataGrid bound to an ObservableCollection<DeviceData>.  When the user clicks a button the collection is updated using a background thread.  The DataGrid updates correctly and shows the added rows as expected.  The problem is the validation code it not showing the error for the row.  If I edit the row from the datagrid cell the validation occurs correctly and is shown.

    Is there a way to force the validation to update after importing the new rows? 

        <l:DataLink x:Key="dl" />
        <Style TargetType="{x:Type DataGridRow}">
                <Trigger Property="Validation.HasError"
                    <Setter Property="Background"
                            Value="Red" />
    <DataGrid AutoGenerateColumns="False"
              ItemsSource="{Binding Path=Devices, UpdateSourceTrigger=PropertyChanged}">
            <ds:DeviceDataValidationRules ValidationStep="UpdatedValue" />

    John J. Hughes II

    Monday, April 9, 2012 9:27 PM

All replies

  • Hi John,

    Could you show us more code about this issue?

    I see your problem is the validation works when edit the DataGridCell but not work when you using backgroundworker to update the source, am I right?

    The BindingValidation only works when the UI try to update the source via DataBinding, if you use backgroundworker to update source, then I think the UI cannot get the notification from the bindingsource, so you could declare a new property to specify a value is valid or not, then use one data trigger to bind this property.

    Have a nice day,

    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us

    Monday, April 16, 2012 10:18 AM
  • Usually the target (control) updates the source (the bound dataitem).
    Errors occuring on the way from target to source are automatically added to the Validation.Errors collection
    (in "default" binding settings). That's why the error template shows up when editing manually.
    If the update of the source occurs through a background thread the bindings validation
    mechanism isn't automatically triggered or notified afaics (see Remarks section of Validation.Error)
    Maybe it helps to set the bindings NotifyOnErrror flag to true (Likely not, however...)
    Otherwise you could explicitly invalidate the binding using the
    MarkInvalid method.

    Monday, April 16, 2012 10:37 AM