如何在父类的自定义用户控件中设置自定义验证错误模板的属性



我有一个自定义用户控件,它带有一个自定义验证错误模板,该模板是一个文本块。我想做的是能够从父控件设置此文本块的高度、宽度和边距。我已经在网上和堆栈溢出上进行了研究,但找不到解决方案。我指的是用户控件中的这一行。我尝试过绑定和模板绑定,但没有成功

<TextBlock Name="ErrorBorder"  Width="10" Height="26" Background="Red" Margin="0,0,2,2"> </TextBlock>

有没有一种方法可以在用户控制中设置此文本块的属性

自定义用户控制

<UserControl x:Class="LearnValidationWthCustomUserCtrl.LabelTextUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:local="clr-namespace:LearnValidationWthCustomUserCtrl"
mc:Ignorable="d" 
d:DesignHeight="450" d:DesignWidth="800"
x:Name="parent"
>
<Grid x:Name="Grid" >
<StackPanel Orientation="Vertical" x:Name="LayoutRoot" >
<TextBlock Text="{Binding TopLabelText}" FontSize="20" 
></TextBlock>
<StackPanel Orientation="Horizontal">
<TextBlock  Text="{Binding LeftLabelText}" FontSize="20" VerticalAlignment="Center" 
Margin="5,0,5,0" Width="{Binding LeftLabelWidth}"
></TextBlock>
<TextBox Name="MyTextBox" Text="{Binding TextBoxText,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" 
Width="{Binding TextBoxWidth}"  FontSize="20"  VerticalAlignment="Center"
BorderBrush="Black" BorderThickness="1" 
MaxLength="{Binding MaxLength}" Height="{Binding TextBoxHeight}" Foreground="Green">
<TextBox.Style>
<Style  TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate >
<TextBlock Name="ErrorBorder"  Width="10" Height="26" 
Background="Red" Margin="0,0,2,2" >
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<TextBlock Text="{Binding Units}" FontSize="20" Margin="5,0,0,0"></TextBlock>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>

主窗口.xaml

<Window x:Class="LearnValidationWthCustomUserCtrl.MainWindow"
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:LearnValidationWthCustomUserCtrl"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="Name" Grid.Row="0"></TextBlock>
<TextBox Text="{Binding CustomerName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="1" Height="50" Width="50"></TextBox>
<TextBox Text="{Binding CustomerName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="2" Height="50" Width="50"></TextBox>
<local:LabelTextUC Grid.Row="3" TextBoxText="{Binding CustomerName,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" TextBoxWidth="100"
MaxLength="5"   ></local:LabelTextUC>
</Grid>
</Window>

将TextBlock放入<AdornedElementPlaceholder>会将其大小调整为父TextBox,因此不需要指定宽度&高度:-

<AdornedElementPlaceholder>
<TextBlock Name="ErrorBorder" Background="Red" />
</AdornedElementPlaceholder>

顺便说一句,您不需要触发器,因为WPF会帮您处理这件事,并且只在绑定属性出错时显示错误模板。

另外,为什么要使用TextBlock?每当字段处于错误状态时,这将使其模糊,并阻止用户使用鼠标访问TextBox。如果你试图实现红色背景的效果来指示错误,那么我认为这是不可能的,因为"装饰层"总是绘制在控件的顶部。

错误模板在控件周围绘制红色边界更为常见,例如:

<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<AdornedElementPlaceholder>
<Border BorderBrush="Red" BorderThickness="1" />
</AdornedElementPlaceholder>
</ControlTemplate>
</Setter.Value>
</Setter>

您可以使用DependencyProperties公开UserControl的属性,然后从UserControl内部和外部绑定到这些属性。

DependencyProperty:

public static readonly DependencyProperty ErrorBorderHeightProperty
= DependencyProperty.Register(
"ErrorBorderHeight", typeof(double), typeof(MyUserControl),
new FrameworkPropertyMetadata((double)20, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
| FrameworkPropertyMetadataOptions.AffectsRender));
[TypeConverter(typeof(LengthConverter))]
public double ErrorBorderHeight
{
get { return (double)GetValue(ErrorBorderHeightProperty); }
set { SetValue(ErrorBorderHeightProperty, value); }
}

在您的UserControl:内部

<TextBlock Name="ErrorBorder"
Height="{Binding RelativeSource={RelativeSource AncestorType=local:MyUserControl}, Path=ErrorBorderHeight}"/>

UserControl:之外

<local:MyUserControl ErrorBorderHeight="10"/>

最新更新