Silverlight ValidationSummary不显示异常的小问题

Silverlight ValidationSummary不显示异常的小问题,第1张

概述这两天在做Silverlight输入验证时想使用Silverlight的控件验证及ValidationSummary,但发现控件能根据Model进行验证,但ValidationSummary总不能显示汇总的错误信息,原来是xaml文件中ValidationSummary控件与Grid控件之间放了一个stackPanel导致ValidationSummary不能正常显示验证的汇总信息,将StackP 这两天在做Silverlight输入验证时想使用Silverlight的控件验证及ValIDationSummary,但发现控件能根据Model进行验证,但ValIDationSummary总不能显示汇总的错误信息,原来是xaml文件中ValIDationSummary控件与GrID控件之间放了一个stackPanel导致ValIDationSummary不能正常显示验证的汇总信息,将StackPanel删除掉,将ValIDationSummary控件作为GrID的直接子元素就行了。下面贴上代码。


首先是错误的xaml代码

<GrID name="grIDProductDetails" Background="White" >        <GrID.ColumnDeFinitions>            <ColumnDeFinition WIDth="auto"></ColumnDeFinition>            <ColumnDeFinition></ColumnDeFinition>        </GrID.ColumnDeFinitions>        <GrID.RowDeFinitions>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="*"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>        </GrID.RowDeFinitions>        <TextBlock margin="7">Model Number:</TextBlock>        <TextBox margin="5" GrID.Column="1" WIDth="100" HorizontalAlignment="left" x:name="txtModelNumber"    Text="{Binding ModelNumber,Mode=TwoWay,ValIDatesOnExceptions=True,NotifyOnValIDationError=True}"></TextBox>        <TextBlock margin="7" GrID.Row="1">Model name:</TextBlock>        <TextBox margin="5" GrID.Row="1" GrID.Column="1"   Text="{Binding Modelname,TargetNullValue='[this is null]',NotifyOnValIDationError=True}"></TextBox>        <TextBlock margin="7" GrID.Row="2">Unit Cost:</TextBlock>        <TextBox margin="5" GrID.Row="2" GrID.Column="1" x:name="txtUnitCost" WIDth="100" HorizontalAlignment="left"   Text="{Binding UnitCost,Mode=TwoWay}"></TextBox>        <TextBlock margin="7,7,0" GrID.Row="3">Description:</TextBlock>        <TextBox margin="5" GrID.Row="3" GrID.Column="1"   Text="{Binding Modelname2,FallbackValue='N/A load Failed'}"></TextBox>        <TextBox margin="7,9" GrID.Row="4" GrID.ColumnSpan="2"   Text="{Binding Description,Mode=TwoWay}" textwrapPing="Wrap"></TextBox>                <StackPanel margin="7" GrID.Row="5" GrID.ColumnSpan="2">            <TextBlock>汇总验证信息:</TextBlock>        <sdk:ValIDationSummary x:name="valIDationSummary" MinHeight="50" />        <!--注意,必须将ValIDationSummary放在GrID的下一级,不能在ValIDationSummary和GrID间加上其他容器控件,否则不触发-->        </StackPanel>        <TextBlock  margin="7" GrID.Row="6" GrID.ColumnSpan="2" Foreground="Red" FontWeight="Bold" x:name="lblinfo" ></TextBlock>                <button GrID.Row="7" x:name="btnCheck" Click="btnCheck_Click">测试</button>    </GrID>


再是正确的xaml代码

<GrID name="grIDProductDetails" Background="White" >        <GrID.ColumnDeFinitions>            <ColumnDeFinition WIDth="auto"></ColumnDeFinition>            <ColumnDeFinition></ColumnDeFinition>        </GrID.ColumnDeFinitions>        <GrID.RowDeFinitions>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="*"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>            <RowDeFinition Height="auto"></RowDeFinition>        </GrID.RowDeFinitions>        <TextBlock margin="7">Model Number:</TextBlock>        <TextBox margin="5" GrID.Column="1" WIDth="100" HorizontalAlignment="left" x:name="txtModelNumber"    Text="{Binding ModelNumber,Mode=TwoWay}" textwrapPing="Wrap"></TextBox>                <sdk:ValIDationSummary x:name="valIDationSummary" MinHeight="50" margin="7" GrID.Row="5" GrID.ColumnSpan="2"/>        <!--注意,必须将ValIDationSummary放在GrID的下一级,不能在ValIDationSummary和GrID间加上其他容器控件,否则不触发-->        <TextBlock  margin="7" GrID.Row="6" GrID.ColumnSpan="2" Foreground="Red" FontWeight="Bold" x:name="lblinfo" ></TextBlock>                <button GrID.Row="7" x:name="btnCheck" Click="btnCheck_Click">测试</button>    </GrID>


正确的后台代码

public partial class MainPage : UserControl    {        public MainPage()        {            InitializeComponent();        }        private voID UserControl_Loaded(object sender,RoutedEventArgs e)        {            //方式1,通过后台代码设置数据源            Product product = new Product("AEFS100","Portable",77,"Analyzes the electrical activity of a person's heart and applIEs " +"an electric shock if necessary.");            grIDProductDetails.DataContext = product;            valIDationSummary.DataContext = product;        }        private voID btnCheck_Click(object sender,RoutedEventArgs e)        {            Product product= grIDProductDetails.DataContext as Product;            lblinfo.Text = "Model name: " + product.Modelname + "\nModel Number: " +product.ModelNumber + "\nUnit Cost: " + product.UnitCost;        }    }

public class Product    {        private string modelNumber;        [StringLength(25)]        [required(ErrorMessage="型号是必填字段")]        public string ModelNumber        {            get { return modelNumber; }            set {                //if (value==null || string.IsNullOrEmpty(value.ToString()))                 //{                //    throw new Exception("抛出异常,型号是必填字段");                //}                ValIDationContext context = new ValIDationContext(this,null,null);                context.Membername = "ModelNumber";                ValIDator.ValIDateProperty(value,context);                modelNumber = value; }        }        private string modelname;        [StringLength(26)]        [required()]        public string Modelname        {            get { return modelname; }            set             {                ValIDationContext context = new ValIDationContext(this,null);                context.Membername = "Modelname";                ValIDator.ValIDateProperty(value,context);                modelname = value;             }        }        private double unitCost;        public double UnitCost        {            get { return unitCost; }            set { unitCost = value; }        }        private string description;        public string Description        {            get { return description; }            set { description = value; }        }        public Product(string modelNumber,string modelname,double unitCost,string description)        {            ModelNumber = modelNumber;            Modelname = modelname;            UnitCost = unitCost;            Description = description;        }        public Product() { }    }
总结

以上是内存溢出为你收集整理的Silverlight ValidationSummary不显示异常的小问题全部内容,希望文章能够帮你解决Silverlight ValidationSummary不显示异常的小问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1013580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存