WPF 样式覆盖破坏了验证错误事件传播
我有一个覆盖 Window: 的自定义控件:
public class Window : System.Windows.Window
{
static Window()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Window), new System.Windows.FrameworkPropertyMetadata(typeof(Window)));
}
...
}
它还有一个样式:
<Style TargetType="{x:Type Controls:Window}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="WindowStyle"
Value="None" />
<Setter Property="Padding"
Value="5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:Window}">
...
不幸的是,这会破坏窗口内容的 Validation.ErrorEvent 传播。也就是说,我的窗口可以很好地接收事件,但我不知道如何处理它来模仿标准窗口(或任何人)处理它的方式。
如果验证控件放置在标准窗口中,它们就可以工作。如果我只是取出 OverrideMetadata 调用(将它们留在我的自定义窗口中),它们也可以工作。如果我离开 OverrideMetadata 调用,但不定义自定义 ControlTemplate,它们也可以工作。如果我将模板保留为默认模板,则内部的内容会正确接收其验证事件并使用其验证模板。
为什么会发生这种情况?如何使用自定义控件模板使处理这些验证错误事件的库存功能再次工作?
谢谢!
I have a custom control that overrides Window:
public class Window : System.Windows.Window
{
static Window()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Window), new System.Windows.FrameworkPropertyMetadata(typeof(Window)));
}
...
}
It also has a style:
<Style TargetType="{x:Type Controls:Window}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="WindowStyle"
Value="None" />
<Setter Property="Padding"
Value="5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:Window}">
...
Unfortunately, this breaks the propagation of the Validation.ErrorEvent for my window's contents. That is, my window can receive the event just fine, but I don't know what to do with it to mimic how a standard Window (or whoever) deals with it.
If the validating controls are placed in a standard window, they work. They also work if I just take out the OverrideMetadata call (leaving them inside my custom window). They also work if I leave the OverrideMetadata call, but don't define a custom ControlTemplate. If I leave the template as the default one, the stuff inside properly receive their validation events and use their validation templates.
Why is this happening, and how can I get the stock functionality for handling these validation error events working again, using a custom control template?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下 ControlTemplate for Window 对我有用:
如果删除 AdornerDecorator,验证错误将不可见
Following ControlTemplate for Window works for me:
Validation errors will be invisible if you remove AdornerDecorator