首先是错误的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不显示异常的小问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)