WPF- Validation - 由于 AdornerDecorator,验证错误消息位于其他控件后面

发布于 2024-10-09 12:55:40 字数 3253 浏览 3 评论 0原文

我在 ViewModel 中实现了 IDataErrorInfo,以便在文本框有错误时返回一个字符串。

    public string this[string columnName]
    {
        get { return "Error-- This is a long error message - sd"; }
    }

但此错误消息位于 UI 上其他控件的后面,如下所示。

alt text

下面是 xaml:

<Window x:Class="Test.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="600" Width="600">

<Window.Resources>        
    
    <ControlTemplate x:Key="validationTemplateNew">
        <DockPanel LastChildFill="True">
            <TextBlock Name="ErrorText" DockPanel.Dock="Bottom" Foreground="White" Background="Red" 
                                   FontSize="12" Padding="2" FontFamily="Trebuchet MS" 
                                   Margin="5,5,0,0"                                        
                                   TextWrapping="Wrap"                                        
                                   Text="{Binding [0].ErrorContent}" ></TextBlock>
            <AdornedElementPlaceholder Name="ErrorTextBox" />
        </DockPanel>
    </ControlTemplate>
    <Style x:Key="ValidationStyle" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="BorderBrush" Value="Red" />
                <Setter Property="BitmapEffect">
                    <Setter.Value>
                        <BitmapEffectGroup>
                            <OuterGlowBitmapEffect GlowColor="Red" GlowSize="3" Noise="0.6"></OuterGlowBitmapEffect>
                        </BitmapEffectGroup>
                    </Setter.Value>
                </Setter>                    
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <ItemsControl Name="ItemCtrl">

        <AdornerDecorator>
            <TextBox 
             FontSize="11" 
             Margin="10" 
             Width="250"      
             VerticalAlignment="Center"                                         
             Text="{Binding Path=StrText, ValidatesOnDataErrors=True, 
                    UpdateSourceTrigger=PropertyChanged}" 
             Validation.ErrorTemplate="{StaticResource validationTemplateNew}"
            Style="{StaticResource ValidationStyle}"
             
             >
            </TextBox>
        </AdornerDecorator>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
    </ItemsControl>        
</Grid>

</Window>

请让我知道如何使用 AdornerDecorator 以使错误消息与其他控件重叠并且不会不要落后。

我的应用程序是这样的,如果我不使用AdornerDecorator,则根本不会显示错误消息。

I have implemented IDataErrorInfo in my ViewModel to return a string if the text box has error.

    public string this[string columnName]
    {
        get { return "Error-- This is a long error message - sd"; }
    }

But this error message goes behind the other control on the UI as shown below.

alt text

Below is the xaml:

<Window x:Class="Test.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="600" Width="600">

<Window.Resources>        
    
    <ControlTemplate x:Key="validationTemplateNew">
        <DockPanel LastChildFill="True">
            <TextBlock Name="ErrorText" DockPanel.Dock="Bottom" Foreground="White" Background="Red" 
                                   FontSize="12" Padding="2" FontFamily="Trebuchet MS" 
                                   Margin="5,5,0,0"                                        
                                   TextWrapping="Wrap"                                        
                                   Text="{Binding [0].ErrorContent}" ></TextBlock>
            <AdornedElementPlaceholder Name="ErrorTextBox" />
        </DockPanel>
    </ControlTemplate>
    <Style x:Key="ValidationStyle" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="BorderBrush" Value="Red" />
                <Setter Property="BitmapEffect">
                    <Setter.Value>
                        <BitmapEffectGroup>
                            <OuterGlowBitmapEffect GlowColor="Red" GlowSize="3" Noise="0.6"></OuterGlowBitmapEffect>
                        </BitmapEffectGroup>
                    </Setter.Value>
                </Setter>                    
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <ItemsControl Name="ItemCtrl">

        <AdornerDecorator>
            <TextBox 
             FontSize="11" 
             Margin="10" 
             Width="250"      
             VerticalAlignment="Center"                                         
             Text="{Binding Path=StrText, ValidatesOnDataErrors=True, 
                    UpdateSourceTrigger=PropertyChanged}" 
             Validation.ErrorTemplate="{StaticResource validationTemplateNew}"
            Style="{StaticResource ValidationStyle}"
             
             >
            </TextBox>
        </AdornerDecorator>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
        <TextBox Width="250" Text="ASDFASFASDFASDFASDFASDFASDF"/>
    </ItemsControl>        
</Grid>

</Window>

Please let me know how to use AdornerDecorator such that the error message overlaps the other controls and doesn't go behind.

My application is such that if I don't use AdornerDecorator, the error message is not displayed at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ弥猫深巷。 2024-10-16 12:55:40

在 AdornerDecorator 上添加 Grid.ZIndex 应该就足够了

<Grid>
    <ItemsControl Name="ItemCtrl">
        <AdornerDecorator Grid.ZIndex="1">

Adding Grid.ZIndex on the AdornerDecorator should be enough

<Grid>
    <ItemsControl Name="ItemCtrl">
        <AdornerDecorator Grid.ZIndex="1">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文