Vinci
Vinci

Reputation: 130

Positioning texblocks in control template

Im trying to make exclamation mark next to the text for validation exception. There is my custom template there is my 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>

I want the exclamation mark on the left side of the text, I can't reach it anyway i try, with stack panel and it's orientation changed, dock panels etc. With this code it looks that: How It looks like

in some different variables it can be on the left side of the textbox Any tips?

Upvotes: 0

Views: 198

Answers (1)

Massimiliano Kraus
Massimiliano Kraus

Reputation: 3835

Try to wrap the Border with Background="Red" and the TextBlock with Foreground="Red" in a StackPanel with 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>

This worked for me.

PS: did you realize that you wrote MinHeight="90"... and then Height="80"? Does it make sense for you?

Upvotes: 0

Related Questions