首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Validation.ErrorTemplate样式问题

Validation.ErrorTemplate样式问题
EN

Stack Overflow用户
提问于 2010-06-23 09:32:40
回答 1查看 12.5K关注 0票数 6

当文本框的内容无效时,我尝试将样式应用于该文本框。

以下样式有效:

代码语言:javascript
复制
     <Style x:Key="textBoxNormalStyle" TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="ForceCursor" Value="False"/>
    <Setter Property="Foreground" Value="{StaticResource TextColor}"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="3,0,3,0"/>
    <Setter Property="Margin" Value="2"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Background="{StaticResource TextBackColor}" BorderBrush="{StaticResource BorderColor}" x:Name="Bd" CornerRadius="4" BorderThickness="2">
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
<Setter Property="Validation.ErrorTemplate">
  <Setter.Value>
    <ControlTemplate>
    </ControlTemplate>
  </Setter.Value>
</Setter>
<Style.Triggers>
  <Trigger Property="Validation.HasError" Value="True">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="ForceCursor" Value="False"/>
    <Setter Property="Foreground" Value="{StaticResource TextColor}"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="3,0,3,0"/>
    <Setter Property="Margin" Value="2"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBox}">
          <Grid Name="test">
            <Border Background="{StaticResource TextBackColor}" BorderBrush="#d99" x:Name="Bd" CornerRadius="4" BorderThickness="2">
              <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
            </Border>
            <Image Name="ErrorImage" Width="24" Height="24" Margin="0,0,4,0" Source="/Images/error.png" HorizontalAlignment="Right">
            </Image>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Trigger>
</Style.Triggers>

但是,我希望去掉上面的触发器,并将触发器中的控件模板移动到Validation.ErrorTemplate部分。但当我这样做时,错误模板的控件不能正确显示(错误模板的大小默认为其中包含的图像的大小,左对齐,并阻止用户可能在文本框中键入的文本)。

对于上面的xaml,有没有更干净的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-23 11:07:36

ErrorTemplate是一个adorner,而不是控件模板的替代品。在模板中包含一个要为原始控件留出空间的AdornedElementPlaceholder。因此,您的错误模板应该如下所示:

代码语言:javascript
复制
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <Grid Name="test">
                <AdornedElementPlaceholder/>
                <Image Name="ErrorImage" Width="24" Height="24" Margin="0,0,4,0" Source="/Images/error.png" HorizontalAlignment="Right"/>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

由于您还希望更改边框的颜色,因此我将使用边框画笔的TemplateBinding并在Validation.HasError触发器中进行更改:

代码语言:javascript
复制
<Setter Property="BorderBrush" Value="{StaticResource BorderColor}"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBox}">
            <Border Background="{StaticResource TextBackColor}" BorderBrush="{TemplateBinding BorderBrush}" x:Name="Bd" CornerRadius="4" BorderThickness="2">
                <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="True">
        <Setter Property="BorderBrush" Value="#d99"/>
    </Trigger>
</Style.Triggers>
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3098340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档