这两天在做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,Mode=TwoWay,TargetNullValue='[this is null]',ValidatesOnExceptions=True,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,7,0" Grid.Row="3">Description:</TextBlock>
<TextBox Margin="5" Grid.Row="3" Grid.Column="1"
Text="{Binding ModelName2,Mode=TwoWay,TargetNullValue='[this is null]',FallbackValue='N/A load failed'}"></TextBox>
<TextBox Margin="7,7,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,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,Mode=TwoWay,TargetNullValue='[this is null]',ValidatesOnExceptions=True,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,7,0" Grid.Row="3">Description:</TextBlock>
<TextBox Margin="5" Grid.Row="3" Grid.Column="1"
Text="{Binding ModelName2,Mode=TwoWay,TargetNullValue='[this is null]',FallbackValue='N/A load failed'}"></TextBox>
<TextBox Margin="7,7,7,9" Grid.Row="4" Grid.ColumnSpan="2"
Text="{Binding Description,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, 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() { }
}