自定义验证数据绑定 [英] Custom validation Data Binding

查看:62
本文介绍了自定义验证数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有这个XAML

Hi everybody,

I have this XAML

<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>



我想知道如何将标记行的值绑定到ValidationRule的ErrorMessage属性。



I考虑与RelativeSource = self进行绑定,但我已经停在这里了。有什么想法吗?


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?

推荐答案



< span class =code-attribute> ErrorMessage = 电子邮件无效! / >
< / Binding .ValidationRules >
< Binding.ValidatesOnDataErrors > True < /Binding.ValidatesOnDataErrors >
< / Binding >
< / TextBox.Text >
< TextBox.Style >
< 样式 TargetType = TextBox >
< Style.Triggers >
< 触发器 属性 = Validation.HasError = True >
< Setter 属性 = < span class =code-keyword>工具提示 = 有错误! / > < -----

< / Trigger >
< / Style.Triggers >
< / Style >
< / TextBox.Style >
< / TextBox >
< CheckBox 内容 = CheckBox Horizo​​ntalAlignment = 保证金 = 10,42,0,0 VerticalAlignment = Top / < span class =code-keyword>>


< / Grid >
< / Window >
" 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>



我想知道如何绑定标记行的值到ValidationRule的ErrorMessage属性。



我想到了一个与RelativeSource = self的绑定,但我已经停在这里了。有什么想法?


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?


这篇关于自定义验证数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆