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
相关推荐
The Sheep 202315 小时前
WPF自定义路由事件
大数据·hadoop·wpf
阳光雨滴15 小时前
使用wpf用户控件编程落石效果动画
c++·wpf
wuty00715 小时前
WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离
wpf·sendmessage·changewindowmessagefilterex·uip·消息筛选器的用户界面特权隔离·window message
攻城狮CSU1 天前
WPF中核心接口 INotifyPropertyChanged
wpf
c#上位机1 天前
wpf之Interaction.Triggers
c#·wpf
是木子啦1 天前
wpf passwordbox控件 光标移到最后
c#·wpf
The Sheep 20231 天前
wpf 命令理解
wpf
布伦鸽1 天前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf
分布式存储与RustFS2 天前
告别复杂配置:用Milvus、RustFS和Vibe Coding,60分钟DIY专属Chatbot
wpf·文件系统·milvus·对象存储·minio·rustfs·vibe
攻城狮CSU2 天前
WPF 绑定机制实现原理
wpf