WPF自定义窗口 输入验证不生效

WPF自定义窗口 输入验证不生效

  • [WPF ValidationRule 不生效](#WPF ValidationRule 不生效)

WPF ValidationRule 不生效

解决方案:在WindowStyle的Template中添加AdornerDecorator标签。

xml 复制代码
<Style x:Key="WindowStyle1" TargetType="{x:Type Window}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <AdornerDecorator>
                    <ContentPresenter />
                </AdornerDecorator>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

为什么官网例子,运行没问题,放到自己的程序里,验证提示就不生效?

是因为现在新版vs或blend的WindowStyle代码模板去掉了一些重要的东西,其中就包含AdornerDecorator。而自带的window窗口样式中包含了AdornerDecorator标签,因此你用官网的例子没有问题,放到你的自定义窗口项目里就始终无法显示。

官方示例:

  1. https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.controls.validationrule?view=windowsdesktop-9.0
  2. https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.controls.adornedelementplaceholder?view=windowsdesktop-9.0
  3. https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.documents.adornerdecorator?view=windowsdesktop-9.0
相关推荐
武藤一雄1 天前
WPF处理耗时操作的7种方法
microsoft·c#·.net·wpf
Venom841 天前
我的 WPF Powermill 工具
wpf
一念春风2 天前
证件照制作工具(WPF C#)
c#·wpf
He BianGu3 天前
【笔记】在WPF中GiveFeedbackEventHandler的功能和应用场景详细介绍
笔记·wpf
就是有点傻3 天前
WPF自定义控件-水晶球
wpf
He BianGu3 天前
【笔记】在WPF中QueryContinueDragEvent的详细介绍
笔记·wpf
He BianGu3 天前
【笔记】在WPF中QueryCursor事件的功能和应用场景详细介绍
笔记·wpf
He BianGu3 天前
【笔记】在WPF中CommandManager的功能和应用场景详细介绍
笔记·wpf
关关长语3 天前
HandyControl中Button图标展示多色路径
c#·.net·wpf·handycontrol
baivfhpwxf20234 天前
WPF DataGrid 指定列的数据可以编辑功能开发
wpf