silverlight数据验证问题

lyh8749 2010-07-09 04:51:44
xaml代码:
<UserControl x:Class="SilverlightApplication1.SilverlightControl4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>

<StackPanel Orientation="Horizontal" Margin="5" Grid.Row="0">
<TextBlock x:Name="Title" FontSize="15" Height="30" Text="{Binding Title,Mode=OneWay}"/>
<TextBlock x:Name="Price" FontSize="15" Height="30" Text="{Binding Price,Mode=TwoWay}"/>
<!--NotifyOnValidationError:是否使用错误验证通知 ValidatesOnExceptions:是否报告验证错误-->
<TextBox x:Name="MyTextBox" Margin="5" Width="100" Height="30" FontSize="15" Text="{Binding Price,Mode=TwoWay,NotifyOnValidationError=True,ValidatesOnExceptions=True}" BindingValidationError="MyTextBox_BindingValidationError"></TextBox>
</StackPanel>
<Button Name="MyButton" Content="修改" Click="MyButton_Click" FontSize="15" Width="80" Height="30" Grid.Row="1"></Button>
</Grid>
</UserControl>


cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace SilverlightApplication1
{
public partial class SilverlightControl4 : UserControl
{
Book book = new Book();
public SilverlightControl4()
{
InitializeComponent();
book.Title = "数据验证测试";
book.Price = 22.22;
this.Title.DataContext = book;
this.Price.DataContext = book;
this.MyTextBox.DataContext = book;
}

private void MyTextBox_BindingValidationError(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
{
MyTextBox.Background = new SolidColorBrush(Colors.Red);
}
else if (e.Action == ValidationErrorEventAction.Removed)
{
MyTextBox.Background = new SolidColorBrush(Colors.White);
}
}

private void MyButton_Click(object sender, RoutedEventArgs e)
{
book.Price = Convert.ToDouble(this.MyTextBox.Text);
}
}

public class Book : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _title;

public string Title
{
get { return _title; }
set { _title = value; }
}
private double _price;

public double Price
{
get { return _price; }
set
{
if (value < 0)
{
throw new Exception("单价不能为负数!");
}
_price = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Price"));
}
}
}
}


运行的时候虽然有效果,但有一个异常,每次都会弹出一个调试框来,不知道大家有没有碰到过。
...全文
119 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyh8749 2010-07-10
  • 打赏
  • 举报
回复
果然是。多谢两位了。我看的是魏永超的《silverlight3.0开发详解与最佳实践》。书上就是这样写的。
两位可不可以留下QQ号码,认识一下。
jv9 2010-07-10
  • 打赏
  • 举报
回复
在VS调试屏蔽异常捕获可以按照1楼Sunpire的方法设置;
而你的代码中使用事件激活进行异常验证,需要对客户端控件进行重新绑定,控件会接管验证。

使用MyTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();就可以实现你的要求了。
Sunpire 2010-07-09
  • 打赏
  • 举报
回复
呃,看了一下LZ的代码,原来还有一个 MyButton_Click 事件,把这个事件去掉, TwoWay 绑定的数据验证是不能通过点击按钮来触发的,这样 throw 出来的异常就要由 按钮点击事件来处理了,而不是由 TextBox 自己来呈现了。
lyh8749 2010-07-09
  • 打赏
  • 举报
回复
我不是调试运行的。但还是会有这个错误。
lyh8749 2010-07-09
  • 打赏
  • 举报
回复
我找到了。在调试->选项和设置里面。但好像不行。
Sunpire 2010-07-09
  • 打赏
  • 举报
回复
在 Silverlight 中,通过“添加服务引用”而自动生成的实体class的定义中均是带有 [DebuggerStepThroughAttribute] 的,顾名思义,这是“自动生成的代码”,要和“我的代码”区分开来。
Sunpire 2010-07-09
  • 打赏
  • 举报
回复
楼主用的是 VS2008 还是 VS2010 ,

在 Debug属性中,选中“仅我的代码” 是指在 VS2010(以2010为例)的菜单“工具-->选项”,弹出窗口“调试-->常规”中的“启用‘仅我的代码’”选项。

这个选项的意思就是和[DebuggerStepThroughAttribute]相关联,
意思就是说在 按F5 Debug时,碰到有[DebuggerStepThroughAttribute]标记的代码,
自动跳过设置在代码中的断点(术语称断点不会命中)、同时对于代码中throw的异常也不会在 VS 中弹出提示窗口-- Debugger Step Through 就是这个字面意思。

注意这个选项仅对 F5 Debug 有效,如果是 Release 的,则是无效的。事实上,楼主的代码以 Release 方式运行时也是不会弹出提示窗口的。
lyh8749 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sunpire 的回复:]
设置一下就可以了,

在 Debug属性中,选中“仅我的代码” 就不会在 Debug 时命中带有 [System.Diagnostics.DebuggerStepThroughAttribute()] 标记的一切异常,

LZ的代码还要改为

C# code

[System.Diagnostics.DebuggerStepThroughAttribute()]
public c……
[/Quote]

非常感谢,但我还是有点不太懂。“在 Debug属性中,选中“仅我的代码” 就不会在 Debug 时命中带有 [System.Diagnostics.DebuggerStepThroughAttribute()] 标记的一切异常,”是什么意思?
Sunpire 2010-07-09
  • 打赏
  • 举报
回复
设置一下就可以了,

在 Debug属性中,选中“仅我的代码” 就不会在 Debug 时命中带有 [System.Diagnostics.DebuggerStepThroughAttribute()] 标记的一切异常,

LZ的代码还要改为

[System.Diagnostics.DebuggerStepThroughAttribute()]
public class Book : INotifyPropertyChanged{}


如果是在 DataGrid 中绑定,似乎不用作任何修改和设置也不会弹出异常的对话框来,DataGrid自己处理了(忘了是不是这样的了,LZ可以试一试)

另外,LZ的 MyTextBox_BindingValidationError() 似乎没有什么意义,本身就有这种效果的。

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧