本节我们来整理一下属性绑定的通知以及错误验证,我们通过
<TextBox Text="{Binding TextBlockInfo,UpdateSourceTrigger=PropertyChanged}"/>
来通知后台属性,界面发生了改变,通过
this.SetProperty(ref m_TextBlockInfo, value, "TextBlockInfo");
来通知界面,后台属性发生了改变
<Window x:Class="PrismDemo.XCViews.XCFirstUC"
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"
xmlns:local="clr-namespace:PrismDemo.ViewModels"
mc:Ignorable="d"
Title="FirstUC" Height="450" Width="800">
<Window.Resources>
<ControlTemplate TargetType="{x:Type TextBox}" x:Key="ct">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" SnapsToDevicePixels="True"
CornerRadius="5">
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"
VerticalContentAlignment="Center" Margin="3,5" BorderThickness="0"/>
</Border>
<TextBlock Grid.Row="1" Text="{Binding (Validation.Errors)[0].ErrorContent,RelativeSource={RelativeSource AncestorType=TextBox,Mode=FindAncestor}}"
Foreground="Red" Margin="10,5"
Name="txtError"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Visibility" Value="Visible" TargetName="txtError"/>
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<StackPanel Margin="30">
<TextBox Text="{Binding TextBlockInfo,UpdateSourceTrigger=PropertyChanged}" FontSize="30" Template="{StaticResource ct}"/>
<TextBlock Text="{Binding TextBlockInfo}" FontSize="30"/>
</StackPanel>
</Grid>
</Window>
using Prism.Mvvm;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrismDemo.XCViewModels
{
public class XCFirstUCVM : BindableBase, INotifyDataErrorInfo
{
private string m_TextBlockInfo = "";
public string TextBlockInfo
{
get { return m_TextBlockInfo; }
set
{
this.SetProperty(ref m_TextBlockInfo, value, "TextBlockInfo");
// 基于ErrorsContainer的数据校验方式
if (value == "1231")
{
// 异常消息
ErrorsContainer.SetErrors("TextBlockInfo", new string[] { "输入值无效1231231" });
}
else
{
ErrorsContainer.ClearErrors("TextBlockInfo");
}
}
}
private ErrorsContainer<string> errorsContainer;
public ErrorsContainer<string> ErrorsContainer
{
get
{
if (errorsContainer == null)
errorsContainer = new ErrorsContainer<string>((propName) =>
{
// 异常信息的处理
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propName));
});
return errorsContainer;
}
set { errorsContainer = value; }
}
public XCFirstUCVM()
{
m_TextBlockInfo = "123";
}
/// <summary>
/// INotifyDataErrorInfo 接口方法
/// </summary>
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public bool HasErrors => ErrorsContainer.HasErrors;
public IEnumerable GetErrors(string propertyName) => ErrorsContainer.GetErrors(propertyName);
}
}
效果如下: