0

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

<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>
4

2 回答 2

0

将 TextBlock 放入 an<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>
于 2019-11-15T14:46:16.647 回答
0

您可以公开您的UserControlwith的属性DependencyProperties,然后绑定到来自内部和外部的那些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"/>
于 2019-11-15T14:53:55.407 回答