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