Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I have this XAML
XML
<Window x:Class="_2nd_DataBindingTest.CustomValidation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Valid="clr-namespace:_2nd_DataBindingTest.Validation"
        Title="CustomValidation" Height="300" Width="300">
    <Grid>
        <Grid.Resources>
            <ControlTemplate x:Key="ValidationTemplate2">
                <Border BorderBrush="Red" BorderThickness="5" CornerRadius="7">
                    <AdornedElementPlaceholder />
                </Border>
            </ControlTemplate>
        </Grid.Resources>
        <!--This is an implementation of a custom validation with a Regex
        using the general validation rule in the code behind and pass the parameters here in XAML-->
        <Label Content="Email:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="59,14,0,0" TextWrapping="Wrap"
                 VerticalAlignment="Top" Width="223" Name="txtCV">
            <TextBox.Text>
                <Binding Path="Email" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <Valid:RegexValidation x:Name="rgv"
                            Expression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
                            ErrorMessage="Email is not valid!" />
                    </Binding.ValidationRules>
                    <Binding.ValidatesOnDataErrors>True</Binding.ValidatesOnDataErrors>
                </Binding>
            </TextBox.Text>
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Style.Triggers>
                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="ToolTip" Value="There is an error!" />  <-----
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
        <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="10,42,0,0" VerticalAlignment="Top"/>


    </Grid>
</Window>

I would like to know how to bind the value of the marked line to the ValidationRule's ErrorMessage property.

I thought about a Binding with a RelativeSource=self, but I'm stopped here. Any ideas?
Posted
Comments
LLLLGGGG 28-Feb-14 11:44am    
Any ideas?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900