Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
Validating fields
posted

Hi, I have a list of objects that I'm binding to in my grid, and I'm wondering how to go about validating the input. Each object has some validation rules associated with them, and if I bind a single object to some text boxes, like 

 <TextBox Text="{Binding Path=PostalCode, ValidatesOnDataErrors=True}"/> 

 The default behavior in WPF is the textbox will have a red border. I'm wondering how to duplicate that behavior in the grid.

Is this possible? 

Parents
  • 2070
    posted

    Hi,

     

    Value editors used by the grid has built-in support for specifying value constraints. However it doesn't have any default appearance when the value constraints fail. To specify some type of visual indication, you can specify an editor style that triggers a visual indication via the editor's IsValueValid property. IsValueValid property basically returns whether the current value is valid based on value constraints.

    Here's a xaml snippet that changes the border of the editor to be red when the value is invalid. The snippet also sets the ValueConstraint property of the editor to constraint the value to be between 0 and 100 inclusive.

     

    <dg:XamDataGrid x:Name="dataGrid" Grid.Row="0" DataSource="{DynamicResource GridDataSource}" >

    <dg:XamDataGrid.FieldLayouts>

    <dg:FieldLayout IsDefault="true"><dg:FieldLayout.Fields>

     

    <dg:Field Name="MyField" >

    <dg:Field.Settings>

    <dg:FieldSettings EditorType="{x:Type dge:XamTextEditor}">

    <dg:FieldSettings.EditorStyle>

    <Style TargetType="{x:Type dge:ValueEditor}">

    <Style.Setters>

    <Setter Property="ValueConstraint">

    <Setter.Value>

    <dge:ValueConstraint MinInclusive="0" MaxInclusive="100" />

    </Setter.Value>

    </Setter>

    <Setter Property="InvalidValueBehavior" Value="RetainValue" />

    </Style.Setters>

    <Style.Triggers>

    <Trigger Property="IsValueValid" Value="false">

    <Trigger.Setters>

    <Setter Property="BorderBrush" Value="Red" />

    </Trigger.Setters>

    </Trigger>

    </Style.Triggers>

    </Style>

    </dg:FieldSettings.EditorStyle>

    </dg:FieldSettings>

    </dg:Field.Settings></dg:Field>

     

    </dg:FieldLayout.Fields>

    </dg:FieldLayout>

    </dg:XamDataGrid.FieldLayouts>

    </dg:XamDataGrid>

     

    Hope this helps,

    Sandip

     

Reply Children