【问题标题】:XAML GridRow doesn't scale automatically when ErrorTemplate is displayed显示 ErrorTemplate 时 XAML GridRow 不会自动缩放
【发布时间】:2016-11-04 20:23:55
【问题描述】:

我有一个带有 TextBox 的 XAML 窗口,而这个 TextBox 有一个 ErrorTemplate。

ErrorTemplate 如下所示,如您所见,我有一个 AdornedElementPlaceholder,后跟一个文本框,其 Text 字段绑定到 ErrorContent:

<ControlTemplate x:Key="ValidationErrorTemplateTextBlock" TargetType="{x:Type Control}">
    <Border BorderBrush="Red" BorderThickness="1">
        <StackPanel Orientation="Vertical">
            <AdornedElementPlaceholder Name="AdornedElementPlaceholder" />
            <TextBlock Text="{Binding ElementName=AdornedElementPlaceholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" 
                    FontSize="10"
                    Background="Red"
                    Foreground="White"
                    Padding="2" />
        </StackPanel>
    </Border>
</ControlTemplate>

<TextBox IsEnabled="{Binding SendMessage}"
                    Text="{Binding AutoMessageSubject, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                    Style="{StaticResource StyleBase}"
                    Validation.ErrorTemplate="{StaticResource ValidationErrorTemplateTextBlock}"
                    HorizontalAlignment="Stretch"
                    Grid.Row="3"
                    Grid.Column="1"
                    Grid.ColumnSpan="2" />

这很好用,除了一件事:TextBox 在 GridRow 内,Height="Auto"。该行根据文本框自行缩放,但是当出现 ErrorTemplate 时,底部有一个额外的 TextBox - GridRow 不会放大以包含新的 TextBox,并且新的 TextBox 会与其下方的元素重叠。

我该如何解决这个问题?

【问题讨论】:

    标签: c# wpf xaml errortemplate


    【解决方案1】:

    Validation.ErrorTemplate:获取或设置用于在装饰层上生成验证错误反馈的 ControlTemplate。

    这意味着如果您使用Validation.ErrorTemplate,验证错误将显示在高于常规内容的图层上,因此“第二个”TextBlock 显示在网格上方,而不是网格单元格内。

    我将实现INotifyDataErrorInfo 而不是半过时的IDataErrorInfo,使用自定义文本框样式,并将第二个TextBlock 的可见性绑定到HasErrors 属性。下面的示例使用 ToolTip 而不是第二个 TextBlock:

           <Style TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="True">
                        <Setter Property="ToolTip">
                            <Setter.Value>
                                <ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}">
                                    <ItemsControl DisplayMemberPath="ErrorContent" ItemsSource="{Binding Path=(Validation.Errors)}" />
                                </ToolTip>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
    

    【讨论】:

    • 很棒的答案。谢谢你。稍后我会对此进行测试。
    【解决方案2】:

    您可能想尝试添加行/列定义:

    <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
    

    【讨论】:

    • 将它们添加到什么位置?控件模板?
    • 周围的
    猜你喜欢
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多