WPF 中 UserControl作为另一个Process宿主到Window里, ErrorTemplate的默认红框没有出现

摘要:
最近做WPF项目遇到一个问题,我有2个process,一个Process里只有Usercontrol,另一个Process获取前一个Process中Usercontrol并host到当前的window里。结果Usercontrol里的ErrorTemplate默认的红框没有出现,但是ValidationRule已经触发。主要是因为UserControl经常应用在Window里或者其他上下文已经有了AdornerLayer。
最近做WPF项目遇到一个问题, 我有2个process, 一个Process里只有Usercontrol, 另一个Process获取前一个Process中Usercontrol并host到当前的window里。 结果Usercontrol里的ErrorTemplate默认的红框没有出现, 但是ValidationRule已经触发。

原因找见: Window类默认的Style包含AdornerDecorator元素, 而UserControl没有。 主要是因为UserControl经常应用在Window里或者其他上下文已经有了AdornerLayer。


解决办法: 在UserControl的逻辑树的根下添加AdornerDecorator, 如:
<UserControl>
<AdornerDecorator>
<Grid Background="Yellow">
...
</Grid>
</AdornerDecorator>
</UserControl>

还需要把子控件的Margin设置下, 腾出空间显示ErrorTemplate。

There is no AdornerLayer in which the error template can be drawn.
Window's default style includes an AdornerDecorator, but UserControl's does not. That's because UserControls are frequently used inside a Window or some other context that already supplies an AdornerLayer.
In your case there is no surrounding AdornerLayer, so you need to add one explicitly. In PASimulationView.xaml:
<AdornerDecorator>
<Grid Background="Yellow">
...
</Grid>
</AdornerDecorator>
You might also want to add a Margin to the TextBox, or do something else to move it away from the top and left edges of the UserControl, so that the top and left edges of the error template are visible.

免责声明:文章转载自《WPF 中 UserControl作为另一个Process宿主到Window里, ErrorTemplate的默认红框没有出现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Windows 桌面主题,桌面背景智能指针处理---bo下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF调用图片路径,或资源图片

一、加载本项目的图片WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源。其中较为常见的情况是用Uri加载图像。Uri表达式的一般形式为:协议+授权+路径协议:pack://授权:有两种。一种用于访问编译时已经知道的文件,用application:///一种用于访问编译时不知道、运行时才知道的文件,用si...

WPF从入门到放弃系列第二章 XAML

本文是作者学习WPF从入门到放弃过程中的一些总结,主要内容都是对学习过程中拜读的文章的整理归纳。 参考资料 XAML 概述 (WPF):https://msdn.microsoft.com/zh-cn/library/ms752059.aspx XAML 语法详述:https://msdn.microsoft.com/zh-cn/library/ms7...

使用WPF动态生成Code 39条形码

最近在看些条形码方面相关的资料,而如果只是看的话,效果似乎并不怎么好,所以决定动手做点Demo,以增强对相关知识的记忆。 这里是一个我编写的使用WPF生成Code 39的例子,Code 39的编码很简单,故而第一次先用它做为尝试。 标准的Code 39只支持43个字符,0~9,A~Z,-,.,$, /, +, %以及空格。除此之外,*用于起始和终止符号。而...

WPF combobox

先写一个数据类Grade.cs usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Collections.ObjectModel; namespace...

WPF笔记(1.2 Navigation导航)——Hello,WPF!

这一节是讲导航的。看了一遍,发现多不能实现,因为版本更新了,所以很多旧的语法不支持了,比如说,不再有NavigationApplication,仍然是Application,TextBlock容器的TextWrap属性改为TextingWrap,StartupUri指向"Page1.xaml"。只要WPFApplication(不是Browser)内展示P...

[WPF](小结2)DataGrid嵌套DataGrid(也叫主从表)

DataGrid嵌套DataGrid(也叫主从表),效果为:单击表中某项后,从中间展开一个新表,总表绑定的是题目类大集合,从表绑定的是对应的选项集合. 第一:选构建题目类,再建一个选项类,题目类集合中的每个项包含一个选项类集合,即数组嵌套数组,C#语句如下: (为方便看清语句,类直接写在主程序中) C#代码如下: usingSystem; using...