Cell Error Visualization
In cases where cell value editing performed via grid cell (represented by ItemCellPresenter object) caused an error (that is the HasError property is True), the grid creates an adorner object above the ItemCellPresenter. A UI of the adorner is defined in the ItemCellErrorAdornerTemplate property (default for all cells) or in the ItemCellErrorAdornerTemplate property, the latter allows you to provide a different template for a specific column. By default it shows a red rectangle around a cell and a ToolTip with a content retrieved from the ItemCellPresenter.ErrorInfo.ErrorContent property. In order to turn off error visualization you should assign the ItemCellErrorAdornerTemplate property with an empty template or a null value.
You can customize the error visualization UI by providing a custom template for the ItemCellErrorAdornerTemplate property of C1DataGrid or Column. The template should use the C1AdornedElementPlaceholder element, which is positioned and sized so as to exactly coincide with an adorned ItemCellPresenter, which is used as a placeholder to relatively position another element in the template. C1AdornedElementPlaceholder is an analogue of the System.Windows.Controls.AdornedElementPlaceholder element used in the System.Windows.Controls.Validation.ErrorTemplate template for WPF Binding error visualization.
The default implementation of the ItemCellErrorAdornerTemplate template looks as follows:
<ControlTemplate x:Key="ItemCellErrorAdornerTemplate" TargetType="Control">
<local:C1AdornedElementPlaceholder Name="placeHolder" >
<Border BorderThickness="1" BorderBrush="Red" CornerRadius="{DynamicResource C1DataGrid_CellBorder_CornerRadius}">
<local:PropertyBridge Source="{Binding ErrorInfo.ErrorContent, Mode=OneWay}" Target="{Binding AdornedElement.ToolTip, ElementName=placeHolder, Mode=OneWayToSource}"/>
</Border>
</local:C1AdornedElementPlaceholder>
</ControlTemplate>
Note that this template assigns the ToolTip property of the adorned ItemCellPresenter presenter (which is done by a PropertyBridge bound to the AdornedElement.ToolTip property), instead of using of the ToolTip property of some element of the template itself. The reason is that the error adorner is not a hit testable element used in order to keep the ability for a user to interact with UI of the ItemCellPresenter decorated by the error adorner. As a result, a ToolTip will not be shown for elements of the error adorner template.
Alternatively, you can customize cell error visualization by providing an updated template for the ItemCellTemplate property. The template may include a trigger for the ItemCellPresenter.HasError property that changes the appearance of the cell, for example the cell's background color, font color, and so on. The ItemCellPresenter.HasError dependency property is a shortcut for the ItemCellPresenter.ItemCell.ErrorInfo.HasError property path and is introduced solely for the purpose of allowing you to use a relatively lightweight Trigger statement instead of slower a DataTrigger in the ItemCellTemplate template.
An example of an updated default ItemCellTemplate template that uses a Trigger based on the HasError property to provide error visualization may look like the following:
<ControlTemplate x:Key="Office2007ItemCellTemplate"
TargetType="local:ItemCellPresenter">
<DockPanel SnapsToDevicePixels="True">
<Rectangle DockPanel.Dock="Right"
Width="{DynamicResource C1DataGrid_GridLineVertical_Thickness}"
Fill="{DynamicResource C1DataGrid_GridLineVertical_Brush}"
VerticalAlignment="Stretch" HorizontalAlignment="Right"
Margin="0,-1,0,-1"/>
<Border Name="brd"
BorderThickness="{DynamicResource C1DataGrid_CellBorder_Thickness}"
BorderBrush="{DynamicResource C1DataGrid_CellBorder_Brush}"
Background="{DynamicResource C1DataGrid_Cell_Brush}"
CornerRadius="{DynamicResource C1DataGrid_CellBorder_CornerRadius}"
TextElement.Foreground="{DynamicResource C1DataGrid_CellText_Brush}">
<local:ItemCellContentPresenter/>
</Border>
</DockPanel>
<ControlTemplate.Triggers>
<!-- ................................................. -->
<!-- Definitions of original triggers that goes here are omitted for clarity; look in the Common.xaml file for a template with the "Office2007ItemCellTemplate" key to see a full definition -->
<Trigger Property="HasError" Value="true">
<Setter TargetName="brd" Property="Background" Value="Red"/>
<Setter TargetName="brd" Property="TextElement.Foreground"
Value="White"/>
<Setter TargetName="brd" Property="ToolTip" Value="{Binding ErrorInfo.ErrorContent}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
|