美文网首页
WPF 绑定数据有效性检查

WPF 绑定数据有效性检查

作者: Ritchie_Li | 来源:发表于2022-06-14 22:29 被阅读0次

(1) 列表控件绑定数据对象集合

<ListBox Name="lstProducts" Margin="5" DisplayMemberPath="ModelName"/>

(2) Grid控件绑定到列表元素,指定为选中的对象,并做错误检查

<Grid Name="gridProductDetails" DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}" Validation.Error="validationError">

设置Grid的资源:

<Grid.Resources>

      <Style TargetType="{x:Type TextBox}">

            <Setter Property="Validation.ErrorTemplate">

                  <Setter.Value>

                      <ControlTemplate>

                          <DockPanel LastChildFill="True">

                              <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="14" FontWeight="Bold"

                                                  ToolTip="{Binding ElementName=adornerPlaceholder,  Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">*</TextBlock>

                    <Border BorderBrush="Green" BorderThickness="1">

                          <AdornedElementPlaceholder Name="adornerPlaceholder"></AdornedElementPlaceholder>

                      </Border>

                  </DockPanel>

        </ControlTemplate>

    </Setter.Value>

</Setter>

<Style.Triggers>

        <Trigger Property="Validation.HasError" Value="true">

              <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"/>

        </Trigger>

</Style.Triggers>

</Style>

</Grid.Resources>

(3) 在Grid中的文本控件绑定对象的属性值,做数据有效性的检查

<TextBox Margin="5" Grid.Column="1">

      <TextBox.Text>

            <Binding Path="ModelNumber" NotifyOnValidationError="true">

              <Binding.ValidationRules>

                    <DataErrorValidationRule></DataErrorValidationRule>

                </Binding.ValidationRules>

            </Binding>

      </TextBox.Text>

  </TextBox>

<TextBox Margin="5" Grid.Row="2" Grid.Column="1">

      <TextBox.Text>

          <Binding Path="UnitCost" NotifyOnValidationError="true" StringFormat="{}{0:C}">

              <Binding.ValidationRules>

                  <local:PositivePriceRule Max="999.99"></local:PositivePriceRule>

                                <ExceptionValidationRule></ExceptionValidationRule>

                </Binding.ValidationRules>

            </Binding>

    </TextBox.Text>

</TextBox>

(4)检查类

using System.Windows.Controls;

using System.Globalization;

namespace WpfApp

{

    public class PositivePriceRule : ValidationRule

    {

        private decimal min = 0;

        private decimal max = Decimal.MaxValue;

        public decimal Min

        {

            get { return min; }

            set { min = value; }

        }

        public decimal Max

        {

            get { return max; }

            set { max = value; }

        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)

        {

            decimal price = 0;

            try

            {

                if (((string)value).Length > 0)

                    //Allow number styles with currency symbols like $

                    price = decimal.Parse((string)value, System.Globalization.NumberStyles.Any);

            }

            catch (Exception e)

            {

                return new ValidationResult(false, "Illegal characters");

            }

            if ((price < min) || (price > Max))

            {

                return new ValidationResult(false, "NOT in the reange " + Min + " to " + Max);

            }

            else

            {

                return new ValidationResult(true, null);

            }

        }

    }

}

效果如下:

相关文章

  • WPF 绑定数据有效性检查

    (1) 列表控件绑定数据对象集合 (2) Grid控件绑定到列表元素,指定为选中的对象,并做错误检查

  • WPF数据绑定

    元素绑定 数据绑定最简单的形式是,源对象是WPF元素而且源属性是依赖属性。依赖项属性具有内置的更改通知支持,当在源...

  • ASP.NET MVC4 模型验证

    模型验证是在模型绑定时检查从HTTP请求接收的数据是否合规以保证数据的有效性,在收到无效数据时给出提示帮助用户纠正...

  • WPF 数据绑定Binding

    自定义Binding 当为Binding设置了继承System.ComponentModel.INotifyPro...

  • WPF 数据绑定(一)

    最基本的绑定 将Text 的文本绑定到Window的Background属性,设置双向绑定,修改文本的值,改变Gr...

  • WPF 数据绑定(二)

    本例演示如何将数据模型的实例绑定到界面控件。 XAML代码如下:

    WPF 数据绑定(三)

    实现绑定一个对象的集合。在界面添加ListBox控件,指定显示对象的属性值。 在后台代码,获取数据集合源,绑定到L...

  • WPF 数据绑定(四)

    实现绑定到DataSet对象。 界面设计: 同样创建Grid的绑定,指定到列表元素,选择的Item

  • WPF 数据绑定(四)

    筛选的数据源的绑定,使用Linq Filter Data Collection。从数据集合中筛选符合设定条件的数据...

  • wpf 中的无效绑定

    设置wpf绑定的跟踪级别为high,output中观察wpf的处理流程: 对于一个无效的绑定,wpf尝试了5次,最...

网友评论

      本文标题:WPF 绑定数据有效性检查

      本文链接:https://www.haomeiwen.com/subject/yjrtvrtx.html