【问题标题】:WPF ControlTemplate HeightWPF 控件模板高度
【发布时间】:2012-06-30 07:05:54
【问题描述】:

我有以下样式来验证我的控件中的输入:

<Style x:Key="MyErrorTemplate" TargetType="Control">
    <Style.Setters>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate x:Name="ControlErrorTemplate">
                    <StackPanel Orientation="Vertical" Height="Auto">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
                            <AdornedElementPlaceholder x:Name="Holder"/>
                        </StackPanel>
                        <Label Foreground="Red" Content="{Binding ElementName=Holder, 
                            Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

如果发生错误,标签中的错误消息会出现在控件下方(例如文本框)并与下方控件重叠。我制作了 StackPanel 的 Height="Auto",但没有帮助。每个控件都在一个 Grid 单元格中,并且 Grid 的行高也是 Auto。 你能告诉我我错过了什么吗?我希望错误消息将下面的内容向下推。 谢谢。

【问题讨论】:

    标签: wpf errortemplate


    【解决方案1】:

    Validation.ErrorTemplate 在装饰层上显示错误反馈。这意味着当布局系统测量和排列装饰元素层上的控件时,不会考虑此模板中的所有控件。

    【讨论】:

    • 感谢您的回答。这是否意味着无法让它按照我的意愿行事?
    • 我认为您必须将标签移动到装饰元素层(在您的控制之下)并使用 Validation.HasError Attached Property 和 BooleanToVisibilityConverter 触发它的可见性。
    【解决方案2】:

    我找到了这个,感谢 LPL,我不知道装饰层。

    我的解决方案是保证金“黑客”。我刚刚使用了触发器:

        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
                <Setter Property="BorderBrush" Value="Red"/>
                <Setter Property="Margin" Value="0,0,0,28"/>
            </Trigger>
        </Style.Triggers> 
    

    增加装饰文本框的下边距。我将边距设置得足够大,以便为单个字符串文本块/标签腾出空间,然后将下面的内容向下移动

    【讨论】:

      猜你喜欢
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      相关资源
      最近更新 更多