2016-05-16 46 views
0

我試圖在驗證異常文本旁邊寫出感嘆號。 還有就是我的自定義模板有我的XAML:在控制模板中定位texblocks

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

我要上文字左側的感嘆號,我也看不達到它,我嘗試,以堆疊面板和它的方向改變,碼頭面板等 有了這個代碼,它看起來是: How It looks like

在一些不同的變量

它可以在文本框 任何提示的左側?

+0

您是否嘗試將StackPanel保留在DockPanel中,並在StackPanel中設置DockPanel.Dock =「Left」Orientation =「Horizo​​ntal」屬性。 – riteshmeher

+0

不幸的是沒有工作:(得到半足夠的解決方案,它看起來很棒,直到有人不會調整它,但我可以關閉尺寸:) – Vinci

回答

0

嘗試用Orientation="Horizontal"包裹BorderBackground="Red"TextBlockForeground="Red"StackPanel

<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"?這對你有意義嗎?