locked
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? 

    <Window.Resources>
        <l:DataLink x:Key="dl" />
    
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError"
                         Value="True">
                    <Setter Property="Background"
                            Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <DataGrid AutoGenerateColumns="False"
              Grid.Row="3"
              Name="DataGrid_Devices"
              CanUserAddRows="False"
              ItemsSource="{Binding Path=Devices, UpdateSourceTrigger=PropertyChanged}">
        <DataGrid.RowValidationRules>
            <ds:DeviceDataValidationRules ValidationStep="UpdatedValue" />
        </DataGrid.RowValidationRules>
        <DataGrid.Columns>
             .....
        </DataGrid.Columns>
    </DataGrid>


    John J. Hughes II
    www.functioninternational.com

    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.

    Chris
    Monday, April 16, 2012 10:37 AM