文本框验证和z-index问题 [英] Textbox validation and z-index problems

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

问题描述

我是WPF的新手,我正在慢慢收集网络上的提示和技巧,以便将我的项目放在一起。我目前无法使用AdornedElementPlaceholder连续获取所有鸭子进行数据验证。



在我的资源档案中我有



I am fairly new to WPF and I am slowly collecting tips and tricks from across the web to put my project together. I am currently having trouble getting all my ducks in a row for data validation using an AdornedElementPlaceholder.

in my resource file I have

<Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <TextBlock DockPanel.Dock="Right" 
                        Foreground="Orange"
                        FontSize="12pt" 
                        ToolTip="{Binding ElementName=MyAdorner, 
                                          Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">***
                        </TextBlock>
                        <Border BorderBrush="Red" BorderThickness="2">
                            <AdornedElementPlaceholder Name="MyAdorner"/>
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                 <Setter Property="Background" Value="MistyRose"/>
            </Trigger>
        </Style.Triggers>
    </Style>



这是我正在使用的文本框,目的是让工作正常工作


and this is the textbox I'm working with currently to make things work

<TextBox Grid.Column="2" Height="23" Name="txtRefreshTime" Width="60" MaxLength="3">
    <TextBox.Text>
        <Binding FallbackValue="59" RelativeSource="{RelativeSource Self}" Path="Text">
            <Binding.ValidationRules>
                <val:StringRangeValidationRule
                    MinimumLength="1"
                    ErrorMessage="Must have a numeric value."/>
                <val:NumericInputValidationRule
                    IsAType="intType"
                    ErrorMessage="Must be a number of seconds."/>
                <val:NumericRangeValidationRule
                    MinimumValue="5"
                    MaximumValue="900"
                    ErrorMessage="Value must be between 5 and 900 seconds (15 minutes)."/>
            </Binding.ValidationRules>
            </Binding>
    </TextBox.Text>
</TextBox>



一旦验证发生并且发现错误我无法点击并将焦点设置在文本框上,一切正常我想要它除外。我可以选择它但我不能点击它。我从这里下载了一个例子,代码没有显示任何不同的东西,它的工作正常。关于我有什么问题或解决问题的方法的任何想法?





我也把这件作品用于资源文件中替代上述。它类似于上面的风格,它也几乎有效,但工具提示没有显示。




Everything works exactly how I want it to EXCEPT once the validation takes place and an error is found I can't click and set focus on the textbox. I can tab to it but I can't click on it. I downloaded an example from here and the code doesn't show anything different and it works just fine. Any ideas on what I have wrong or a way to fix the problem?


I also have used this piece into the resource file as a replacement for the above. It is similar to the above bit of style and it also 'almost' works, but the tooltip doesn't show.

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <AdornedElementPlaceholder Name="controlWithError" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="Background" Value="MistyRose"/>
            <Setter Property="ToolTip"
            Value="{Binding RelativeSource={RelativeSource Self},
            Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>





并根据要求提供一些验证码





And as requested some of the validation code

public class StringRangeValidationRule : ValidationRule
    {
        private int _minimumLength = -1;
        private int _maximumLength = -1;
        private string _errorMessage;

        public int MinimumLength
        {
            get { return _minimumLength; }
            set { _minimumLength = value; }
        }

        public int MaximumLength
        {
            get { return _maximumLength; }
            set { _maximumLength = value; }
        }

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);
            string inputString = (value ?? string.Empty).ToString();
            if (inputString.Length < this.MinimumLength ||
                   (this.MaximumLength > 0 &&
                    inputString.Length > this.MaximumLength))
            {
                result = new ValidationResult(false, this.ErrorMessage);
            }

            return result;
        }
    }

推荐答案

我发现这给了我需要的功能,但我仍然不喜欢知道为什么其他代码不起作用。



I found this gave me the functionality that I need, but I still don't know why the other code didn't work.

<style targettype="{x:Type TextBox}">
        <style.triggers>
           <trigger property="Validation.HasError" value="True">
               <setter property="Background" value="MistyRose" />
               <setter property="ToolTip" value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}" />
           </trigger>
       </style.triggers>
   </style>


这篇关于文本框验证和z-index问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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