【问题标题】:Positioning texblocks in control template在控制模板中定位 texblocks
【发布时间】:2016-05-16 07:33:54
【问题描述】:

我试图在验证异常的文本旁边添加感叹号。 有我的自定义模板,有我的 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>

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

在一些不同的变量中,它可以在文本框的左侧 有什么建议吗?

【问题讨论】:

  • 您是否尝试将 StackPanel 保留在 DockPanel 中并在 StackPanel 中设置 DockPanel.Dock="Left" Orientation="Horizo​​ntal" 属性。
  • 不幸的是不工作:(得到了半足够的解决方案,它看起来很棒,直到有人不会调整它,但我可以关闭调整大小:)

标签: c# .net validation wpf-positioning


【解决方案1】:

尝试将BorderBackground="Red"TextBlockForeground="Red" 包装在StackPanelOrientation="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"?对你有意义吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多