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
相关推荐
weixin_447103582 小时前
Wpf布局之StackPanel!
wpf
小老鼠爱大米2 小时前
[C#] WPF - 资源URI
c#·wpf·uri
三千道应用题10 小时前
WPF学习笔记(13)列表框控件ListBox与数据模板
wpf
甄天10 天前
WPF中MVVM和MVVMLight模式
c#·wpf·visual studio
CoderIsArt12 天前
C# WPF常用调试工具汇总
开发语言·c#·wpf
凉、介12 天前
SylixOS 下的消息队列
linux·wpf·sylixos
Magnum Lehar12 天前
wpf3d游戏引擎ProjectLayoutView实现
游戏引擎·wpf
摆烂的少年12 天前
WPF中自定义DataGrid表格后,修改选中行的字体颜色或背景图
wpf
CoderIsArt12 天前
WPF调试三种工具介绍:Live Visual Tree、Live Property Explorer与Snoop
wpf
甄天12 天前
WPF Style样式 全局样式资源字典
c#·wpf