Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 错误模板显示在其他控件的上方,此时应将其隐藏_.net_Wpf_Validation_Xaml_Idataerrorinfo - Fatal编程技术网

.net 错误模板显示在其他控件的上方,此时应将其隐藏

.net 错误模板显示在其他控件的上方,此时应将其隐藏,.net,wpf,validation,xaml,idataerrorinfo,.net,Wpf,Validation,Xaml,Idataerrorinfo,我试图使用IDataErrorInfo接口在WPF应用程序中实现验证,但遇到了一个不太理想的情况 我有这个模板,当控件无法验证时使用 <ControlTemplate x:Key="errorTemplate"> <DockPanel LastChildFill="true"> <Border Background="Red" DockPanel.Dock="Right" Margin="5,0,0,0" Width="20" Height=

我试图使用
IDataErrorInfo
接口在WPF应用程序中实现验证,但遇到了一个不太理想的情况

我有这个模板,当控件无法验证时使用

<ControlTemplate x:Key="errorTemplate">
    <DockPanel LastChildFill="true">
        <Border Background="Red" DockPanel.Dock="Right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                                    ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
            <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" />
        </Border>
        <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
            <Border BorderBrush="red" BorderThickness="1" />
        </AdornedElementPlaceholder>
    </DockPanel>
</ControlTemplate>

一切正常,直到我尝试在验证失败的控件上方显示某些内容,例如在其上方显示停靠项:

我如何才能避免这种情况,并使我的错误模板显示在dock项下,因为它应该这样做

编辑

我发现我可以用
装饰解码器
包装我的
文本框
来解决这个问题,但我真的不想对应用程序中的每个
文本框
控件都这样做。有没有办法用
样式设置它
或其他方式

编辑2


我可能会更改默认的
TextBox
ControlTemplate以包含
AdornerDecorator
,但我不太喜欢更改任何WPF的默认控件模板。欢迎您提出任何其他建议。

我会使用一种风格,这里是一个您可以轻松适应的风格示例

请注意,ErrorContent来自(Validation.Errors).CurrentItem.ErrorContent,而不是错误[0]。尽管这两种方法都可以工作,但后者会在输出窗口中出现异常


好的,我找到了一个相对简单的解决方案,它不强迫我更改任何控件模板

而不是像这样用
装饰器装饰每个
文本框

<StackPanel>
    <AdornerDecorator>
        <TextBox Text={Binding ...} />
    </AdornerDecorator>
    <AdornerDecorator>
        <TextBox Text={Binding ...} />
    </AdornerDecorator>
</StackPanel>

这样,我可以在每个视图中最多定义一次。

基于@AdiLester伟大的答案,如果您的控件派生自基类,并且您不想在每个控件的XAML中放置
AdorneDecorator
,那么请这样做:

public class MyBaseUserControl : UserControl
{
    public MyBaseUserControl()
    {

    }

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);

        if (!(newContent is AdornerDecorator))
        {
            this.RemoveLogicalChild(newContent);

            var decorator = new AdornerDecorator();
            decorator.Child = newContent as UIElement;

            this.Content = decorator;
        }
    }
}

我看不出这能解决什么问题。边框仍显示在停靠项目的上方。
<AdornerDecorator>
    <StackPanel>
        <TextBox Text={Binding ...} />
        <TextBox Text={Binding ...} />
    </StackPanel>
</AdornerDecorator>
public class MyBaseUserControl : UserControl
{
    public MyBaseUserControl()
    {

    }

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);

        if (!(newContent is AdornerDecorator))
        {
            this.RemoveLogicalChild(newContent);

            var decorator = new AdornerDecorator();
            decorator.Child = newContent as UIElement;

            this.Content = decorator;
        }
    }
}