0

我试图在验证异常的文本旁边制作感叹号。有我的自定义模板有我的 XAML:

<Window x:Class="WpfApplicationLAB.NewGameWindow"
        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:WpfApplicationLAB"
        mc:Ignorable="d"
        Height="80" Width="260"
        WindowStyle="None"
        WindowStartupLocation="CenterScreen"
        AllowsTransparency="False"
        Title="NewGameWindow"
        ResizeMode="CanResize" MinWidth="180" MinHeight="90">
    <Grid Name="GridInputName">
        <Grid.RowDefinitions>
            <RowDefinition Height="25*"/>
            <RowDefinition Height="29*"/>
            <RowDefinition Height="28*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="51*"/>
            <ColumnDefinition Width="121*"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="1" Grid.Column="0" Content="Size:" HorizontalContentAlignment="Center"/>
        <TextBox Name="textBox"  Grid.Row="1" Grid.Column="1">
            <TextBox.Text>
                <Binding Path="Ssize"  UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <local:SizeValidation/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Button Name="Cancel"
            Grid.Row="2" Grid.Column="0" Content="Cancel" Click="Cancel_Click" >
        </Button>
        <Button Name="Ok"
            Grid.Row="2" Grid.Column="1" Content="Ok" Click="Ok_Click">
        </Button>
    </Grid>
    <Window.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel>
                            <Border Background="Red" Margin="0,0,0,0" Width="20" Height="20" CornerRadius="10"
                            ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white">
                                </TextBlock>
                            </Border>
                            <TextBlock
                      Margin="5,0,0,0"
                      Foreground="Red" 
                      Text="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                            </TextBlock>
                            <Border BorderBrush="Red" BorderThickness="1" Margin="5,0,5,0" >
                                <AdornedElementPlaceholder Name="MyAdorner" ></AdornedElementPlaceholder>
                            </Border>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
</Window>

我想要文本左侧的感叹号,无论如何我都无法到达它,堆栈面板及其方向已更改,停靠面板等。使用此代码,它看起来: 它看起来如何

在一些不同的变量中,它可以在文本框的左侧任何提示?

4

1 回答 1

0

尝试将BorderwithBackground="Red"和 the TextBlockwith包装Foreground="Red"StackPanelwith中Orientation="Horizontal"

<Style TargetType="{x:Type TextBox}">
  <Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <StackPanel>
          <StackPanel Orientation="Horizontal">
            <Border Background="Red"
                    Margin="0"
                    Width="20"
                    Height="20"
                    CornerRadius="10"
                    ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
              <TextBlock Text="!"
                         VerticalAlignment="center"
                         HorizontalAlignment="center"
                         FontWeight="Bold"
                         Foreground="white"/>
            </Border>
            <TextBlock Margin="5,0,0,0"
                       Foreground="Red" 
                       Text="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
          </StackPanel>
          <Border BorderBrush="Red" BorderThickness="1" Margin="5,0" >
            <AdornedElementPlaceholder Name="MyAdorner"/>
          </Border>
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

这对我有用。

PS:你有没有意识到你写了MinHeight="90"……然后Height="80"?这对你有意义吗?

于 2016-06-09T11:53:07.117 回答