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
相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘17 小时前
开发WPF项目时遇到的问题总结
wpf
hqwest2 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars2 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest2 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest2 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0073 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度
范纹杉想快点毕业6 天前
C 语言主控开发与显控开发能力体系及技术栈详解,STM32、QT、嵌入式、边缘系统显示
stm32·单片机·tcp/ip·microsoft·fpga开发·51单片机·wpf
weixin_447103586 天前
WPF之绑定!
c#·wpf
DataIntel6 天前
wpf问题记录
wpf
蓝点lilac7 天前
C# WPF 内置解码器实现 GIF 动图控件
c#·.net·wpf·图像