ElementEditingStyle template gets clipped inside DataGrid

Feb 17, 2009 at 6:55 PM

I am attempting to add a validation template to the cells in my DataGrid. When an error occurs in a cell, I would like to show a tooltip-like message showing the error. I am doing this by setting the EditingElementStyle on each of the columns to a ControlTemplate that shows the error in a label next to the cell that has the error. The problem I am getting is that the error message label gets clipped to the bounds of the DataGrid whenever the message is very long, and so the user only gets to see half of the message. I have tried putting the label inside a canvas with ClipToBounds set to false, but the label still gets clipped.

Here is some sample XAML that demonstrates the problem:

<Window x:Class="HandlingWpfDatabindingErrors.Window1"
    Title="Window1" SizeToContent="WidthAndHeight">
    <StackPanel Orientation="Horizontal">
        <Controls:DataGrid ItemsSource="{x:Static HandlingWpfDatabindingErrors:Person.Persons}" AutoGenerateColumns="False" Width="100">
                <Style x:Key="validationTemplate" TargetType="{x:Type TextBox}">
                    <Setter Property="Validation.ErrorTemplate">
                                    <Border BorderBrush="Red" BorderThickness="2" CornerRadius="5">
                                        <AdornedElementPlaceholder x:Name="adorner"></AdornedElementPlaceholder>
                                    <Canvas ClipToBounds="False" Panel.ZIndex="9999">
                                        <Label Margin="2,0,0,0" Foreground="Red" FontWeight="Bold" Background="White" BorderThickness="1"
                       Content="{Binding ElementName=adorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                                <DropShadowBitmapEffect Color="Black" Direction="320" ShadowDepth="10" Softness="0.5" Opacity="0.7"/>
                                                <SolidColorBrush Color="{x:Static SystemColors.WindowFrameColor}"/>
                <Controls:DataGridTextColumn Binding="{Binding Age,ValidatesOnExceptions=True}" Header="Age" EditingElementStyle="{StaticResource validationTemplate}"/>
                <Controls:DataGridTextColumn Binding="{Binding Height,ValidatesOnExceptions=True}" Header="Height" EditingElementStyle="{StaticResource validationTemplate}"/>

        <Button Content="OK"/>       



The underlying class looks like this:

namespace HandlingWpfDatabindingErrors {
    public class Person {


        public static Person[] Persons = new Person[] {
            new Person(1,2),
            new Person(3,4),
            new Person(5,6)};

        public double Age { get; set; }
        public double Height { get; set; }

        public Person(double age, double height) {
            Age = age;
            Height = height;

To reproduce this problem, put a non-numeric value (eg. AAAA) in one of the cells and you will see the message.

There are a couple of problems with this that I would like to fix
1. I would like to show the full message in the label without it getting clipped to the DataGrid bounds.
2. I would like to apply the validationTemplate ElementEditingStyle to all columns in my application automatically, rather than having to explicitly specify this every time.
3. I woud like to set ValidatesOnExceptions=True on all columns in my application rather than having to excplicitly do it on each binding

Any help is greatly appreciated!