我有一个TextBlock
内一个DockPanel
内一个Border
。它TextBlock
是数据绑定的,可以使用屏幕键盘进行更改。因此,我已将其与验证联系起来,并且效果非常好。我的困境是,我似乎只能TextBlock
在出现验证错误时更改 ' 样式。我想触发Border
's 风格。我怎样才能做到这一点?
还有一种方法可以绑定ErrorContents
视图上的其他地方吗?
我有一个TextBlock
内一个DockPanel
内一个Border
。它TextBlock
是数据绑定的,可以使用屏幕键盘进行更改。因此,我已将其与验证联系起来,并且效果非常好。我的困境是,我似乎只能TextBlock
在出现验证错误时更改 ' 样式。我想触发Border
's 风格。我怎样才能做到这一点?
还有一种方法可以绑定ErrorContents
视图上的其他地方吗?
只需从其他控件绑定到您的 TextBox 即可。下面我将 Label 的 Content 属性绑定到 TextBox 的 (Validation.Errors)[0].ErrorContent 属性。
<DockPanel>
<!-- TextBox bound to a data object-->
<TextBox Name="textBox1" DockPanel.Dock="Top" Text="{Binding Path=Age, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<!-- A label that displays the errors from textBox1 -->
<Label Content="{Binding ElementName=textBox1, Path=(Validation.Errors)[0].ErrorContent}"></Label>
</DockPanel>
我认为这将解决您的问题:
xml:
<Window x:Class="WpfErrorsBorderColor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfErrorsBorderColor="clr-namespace:WpfErrorsBorderColor" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="{x:Type WpfErrorsBorderColor:MyControl}" TargetType="{x:Type WpfErrorsBorderColor:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="WpfErrorsBorderColor:MyControl" >
<Border Name="bd" >
<DockPanel LastChildFill="True" >
<TextBox Text="{Binding Path=Description,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Top"/>
<TextBlock Name="textBlock" Text="{Binding Path=Description,ValidatesOnDataErrors=True,NotifyOnValidationError=True}"></TextBlock>
</DockPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger SourceName="textBlock" Property="Validation.HasError" Value="True">
<Setter TargetName="bd" Property="BorderBrush" Value="Red" />
<Setter TargetName="bd" Property="BorderThickness" Value="4" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<WpfErrorsBorderColor:MyControl></WpfErrorsBorderColor:MyControl>
代码:
using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
namespace WpfErrorsBorderColor
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Data data = new Data() { Description = "123" };
DataContext = data;
}
}
public class Data : IDataErrorInfo, INotifyPropertyChanged
{
private string description;
public string Description
{
get { return description; }
set
{
if (description != value)
{
description = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Description"));
}
}
}
string CheckLenght(string str)
{
return (str.Length < 1 || str.Length > 10) ? "Strign must be between 1 and 10 letters" : String.Empty;
}
public string Error
{
get { return CheckLenght(Description); }
}
public string this[string columnName]
{
get { return CheckLenght(Description); }
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public class MyControl:UserControl
{
public MyControl()
{
}
}
}
您可以使用相同的方式绑定ErrorContents ;)