【问题标题】:WPF validation - displaying errors for elements in StackPanelWPF 验证 - 在 StackPanel 中显示元素的错误
【发布时间】:2015-07-05 20:57:01
【问题描述】:

我遇到了如何在垂直 StackPanel 中为 TextBox 元素显示验证错误的问题。我正在尝试在 TextBox 下方显示错误消息。

我有这个错误模板:

    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel>
                        <AdornedElementPlaceholder />
                        <ItemsControl ItemsSource="{Binding}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding (ValidationError.ErrorContent)}" Foreground="Red" Margin="5"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

如果我在 TextBox 下方有足够的空白,则错误会很好地显示,但在 StackPanel(例如)中,由于装饰层,它不会为错误消息添加额外的边距或填充。

预计会是这样,根据this source

请注意,Validation.ErrorTemplate 将显示在装饰层上。装饰层中的元素呈现在其余可视元素之上,并且在布局系统测量和排列装饰元素层上的控件时不会考​​虑它们。

如何显示验证错误消息,以便它们不会显示在 StackPanel 中的其他元素之上?

【问题讨论】:

    标签: wpf validation xaml errortemplate


    【解决方案1】:

    好的,我找到了转换器的解决方案。我最终得到了类似这样的风格:

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Margin" Value="10" />
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel>
                            <AdornedElementPlaceholder />
                            <ItemsControl ItemsSource="{Binding}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding (ValidationError.ErrorContent)}" Foreground="Red" Margin="0,5"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Trigger.Setters>
                        <Setter Property="Margin" Value="{Binding (Validation.Errors).Count, RelativeSource={RelativeSource Self}, Converter={StaticResource ErrorsToMarginConverter}}"/>
                    </Trigger.Setters>
                </Trigger>
            </Style.Triggers>
        </Style>
    

    和转换器:

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
            {
                var errors = (int)value;
                return new Thickness(10, 10, 10, (errors * 20));
            }
    
            return value;
        }
    

    【讨论】:

      【解决方案2】:

      您也可以考虑将您的错误模板包含在 TextBox 的模板中。 类似的东西(当然可以改进):

      <Style x:Key="eTextBox" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="{x:Type TextBox}">
                      <StackPanel>
                          <Border BorderBrush="Gray" BorderThickness="1" CornerRadius="1" Padding="2">
                              <ScrollViewer Name="PART_ContentHost" Focusable="False" 
                                          ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                                          ScrollViewer.VerticalScrollBarVisibility="Hidden"
                                          Background="#00FFFFFF" />
                          </Border>
                          <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(Validation.Errors)}">
                              <ItemsControl.ItemTemplate>
                                  <DataTemplate>
                                      <TextBlock Text="{Binding (ValidationError.ErrorContent)}" Foreground="Red" Margin="5"/>
                                  </DataTemplate>
                              </ItemsControl.ItemTemplate>
                          </ItemsControl>
                      </StackPanel>
      
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
      </Style>
      

      这样,ItemsControl 被考虑用于布局计算。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-11
        • 2013-07-26
        • 2014-02-13
        • 1970-01-01
        • 2017-04-24
        • 2023-03-05
        • 1970-01-01
        相关资源
        最近更新 更多