[Version 1.3]floatingWindow avalondock issues with validation textbox

May 24, 2011 at 12:44 PM
Edited May 24, 2011 at 1:36 PM

 [Version 1.3]   I have a DocumentContent, in which is a textbox with validation.

 

when I make floatingWindow,!(validation) disappears.

Can someone explain to me what the problem is?

  below is the code:

 <ControlTemplate x:Key="validationTemplate">
            <DockPanel LastChildFill="True">
                <TextBlock Foreground="Red"  FontSize="20" FontWeight="Bold"
                           ToolTip="{Binding ElementName=adornerPlaceholder,
                                             Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">!</TextBlock>
                <Border BorderBrush="Green" BorderThickness="1">
                    <AdornedElementPlaceholder Name="adornerPlaceholder">
                    </AdornedElementPlaceholder>
                </Border>
               
            </DockPanel>
            <!--<StackPanel Orientation="Horizontal">
                <TextBlock Foreground="Red" FontSize="22" FontWeight="Bold" Margin="0,0,5,0">!</TextBlock>
                <AdornedElementPlaceholder />
            </StackPanel>-->
            <!--<Border BorderBrush="Red" BorderThickness="2">
                <AdornedElementPlaceholder />
            </Border>-->       

 </ControlTemplate>


        <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="TextBox.ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                                                                          Path=(Validation.Errors)[0].ErrorContent}"/>
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>

 

 

  <avalonDock:DocumentContent Title="Window1.xaml.cs">
                            <StackPanel x:Name="mainPanel" Margin="20">
                                <TextBlock>Age:</TextBlock>
                                <TextBox x:Name="txtAge"
                                         Validation.ErrorTemplate="{DynamicResource validationTemplate}"       
                                         Style="{StaticResource textBoxInError}">
                                   
                                    <Binding Path="Age"
                                             Source="{StaticResource ods}"
                                             UpdateSourceTrigger="PropertyChanged" >
                                        <Binding.ValidationRules>
                                            <!--<ExceptionValidationRule/>-->
                                            <local:AgeRangeRule Min="21" Max="130"/>
                                        </Binding.ValidationRules>
                                    </Binding>
                                </TextBox>
                            </StackPanel>
                        </avalonDock:DocumentContent>

 

 <Window.Resources>
        <local:MyDataSource x:Key="ods"/>

 

 

 public class MyDataSource
    {
        private int _age;
        private int _age2;
        private int _age3;

        public MyDataSource()
        {
            Age = 0;
        }

        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
       
    }

 

 

 public class AgeRangeRule : ValidationRule
    {
        private int _min;
        private int _max;

        public AgeRangeRule()
        {
        }

        public int Min
        {
            get { return _min; }
            set { _min = value; }
        }

        public int Max
        {
            get { return _max; }
            set { _max = value; }
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            int age = 0;

            try
            {
                if (((string)value).Length > 0)
                    age = Int32.Parse((String)value);
            }
            catch (Exception e)
            {
                return new ValidationResult(false, "Illegal characters or " + e.Message);
            }

            if ((age < Min) || (age > Max))
            {
                return new ValidationResult(false,
                  "Please enter an age in the range: " + Min + " - " + Max + ".");
            }
            else
            {
                return new ValidationResult(true, null);
            }
        }
    }