wpf 当我们在验证 controlTemplate 中使用 AdornedElementPlaceholder 时,它究竟做了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24928832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:12:38  来源:igfitidea点击:

What does AdornedElementPlaceholder exactly do when we use it in validation controlTemplate?

c#wpfxaml

提问by Neha

After going through several articles and examples showing usage of

在阅读了几篇文章和示例后,显示了

AdornedElementPlaceholder

装饰元素占位符

i am still confused that what is the exact functionality it incorporates to xaml validation?

我仍然很困惑,它包含在 xaml 验证中的确切功能是什么?

回答by Marcel B

If you use Validations you need to show the user where (and what) failed to validated and that's where AdornedElementPlaceholdercomes into play. It is a Placeholder that has exactly the same size of the UIElement you're validating.

如果您使用验证,您需要向用户显示验证失败的地方(和内容),这就是AdornedElementPlaceholder发挥作用的地方。它是一个与您正在验证的 UIElement 大小完全相同的占位符。

Let's say you're validating user input on a TextBoxand want to show a red box around the TextBoxwhen the validation fails. Define a ValidationRule and a validation template for the TextBox. If the ValidationRule fails, then the Validation.ErrorTemplateis shown on your TextBox. Inside the template the AdornedElementPlaceholdertells the Framework where to place your template on the UI. In our case the template might look like this:

假设您正在验证 a 上的用户输入,TextBox并希望TextBox在验证失败时在周围显示一个红色框。定义 ValidationRule 和验证模板TextBox。如果 ValidationRule 失败,则 将Validation.ErrorTemplate显示在您的TextBox. 在模板内部,它AdornedElementPlaceholder告诉框架在 UI 上放置模板的位置。在我们的例子中,模板可能如下所示:

<ControlTemplate>
    <Border BorderBrush="Red" BorderThickness="1">
        <AdornedElementPlaceholder />
    </Border>
</ControlTemplate> 

You should read this article.

你应该阅读这篇文章

回答by Pheonyx

I believe the AdornedElementPlaceholder is used to show where the "validation indicator element" is positioned in relation to the control being validated. I.e. if you have a text box and when validation fails a red x appears on the right of the box, I believe (although not 100%) that the AdornedElementPlaceholder is responsible for that positioning.

我相信 AdornedElementPlaceholder 用于显示“验证指示器元素”相对于正在验证的控件的位置。即,如果您有一个文本框,并且当验证失败时,框右侧会出现一个红色的 x,我相信(虽然不是 100%)是 AdornedElementPlaceholder 负责该定位。

Represents the element used in a ControlTemplate to specify where a decorated control is placed relative to other elements in the ControlTemplate.

表示在 ControlTemplate 中使用的元素,用于指定装饰控件相对于 ControlTemplate 中其他元素的放置位置。

Source: http://msdn.microsoft.com/en-us/library/system.windows.controls.adornedelementplaceholder(v=vs.110).aspx

来源:http: //msdn.microsoft.com/en-us/library/system.windows.controls.adornedelementplaceholder(v=vs.110).aspx