代码之家  ›  专栏  ›  技术社区  ›  Bigeyes

验证时在文本框中添加警告图标

  •  0
  • Bigeyes  · 技术社区  · 8 年前

    如果出现问题,我想验证文本框。这个想法是,如果出现问题,下一个文本框应该有一个警告图像。

    <TextBox Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}">
        <Validation.ErrorTemplate>
            <ControlTemplate>
                <StackPanel>
                    <!-- Placeholder for the TextBox itself -->
                    <AdornedElementPlaceholder x:Name="textBox"/>
                    <image source="some-Image.png" width="20" Height="20" />
                </StackPanel>
            </ControlTemplate>
        </Validation.ErrorTemplate>
    </TextBox>
    

    但问题是图像没有显示,它只显示图标的边界。 我正在使用 AdornedElementPlaceholder 正确地

    1 回复  |  直到 8 年前
        1
  •  0
  •   XAMlMAX    8 年前

    在出现错误时工作并显示图像的测试解决方案:

    <TextBox BorderThickness="0.8">
        <Validation.ErrorTemplate>
            <ControlTemplate>
                <StackPanel>
                    <AdornedElementPlaceholder/>
                    <Image Source="Image.jpg" Width="20" Height="20"/>
                </StackPanel>
            </ControlTemplate>
        </Validation.ErrorTemplate>
        <TextBox.Text>
            <Binding Path="ValidationTest" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ValidatesOnDataErrors="True">
                <Binding.ValidationRules>
                    <validation:IntegerValidationRule ValidationStep="CommittedValue" Min="1" Max="99999999"/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>  
    

    下面是我在这个例子中使用的验证规则:

    public class IntegerValidationRule : ValidationRule
    {
        private int _min = int.MinValue;
        private int _max = int.MaxValue;
        private string _fieldName = "Field";
        private string _customMessage = String.Empty;
    
        public int Min
        {
            get { return _min; }
            set { _min = value; }
        }
    
        public int Max
        {
            get { return _max; }
            set { _max = value; }
        }
    
        public string FieldName
        {
            get { return _fieldName; }
            set { _fieldName = value; }
        }
    
        public string CustomMessage
        {
            get { return _customMessage; }
            set { _customMessage = value; }
        }
    
    
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            int num = 0;
    
            var val = (value as BindingExpression).DataItem;
    
            if (!int.TryParse(value.ToString(), out num))
                return new ValidationResult(false, $"{FieldName} must contain an integer value.");
    
            if (num < Min || num > Max)
            {
                if (!String.IsNullOrEmpty(CustomMessage))
                    return new ValidationResult(false, CustomMessage);
    
    
                return new ValidationResult(false, $"{FieldName} must be between {Min} and {Max}.");
            }
    
            return new ValidationResult(true, null);
        }
    }
    

    这是刚刚修改的示例 MSDN Docs
    一些注意事项:
    您没有提供验证规则,因此我假设它按预期工作,并生成有效的验证结果。
    您的 TextBox.Text 属性不包括验证规则。

    推荐文章