如何解决在 WPF C# 中扩展 TextBox 以使用 MaxLength
我正在尝试在 WPF 项目中执行此操作: WPF TextBox MaxLength -- Is there any way to bind this to the Data Validation Max Length on the bound field?
也就是说,根据该字段的数据注释扩展 TextBox 以具有默认 MaxLength。
我已将 Microsoft.Xaml.Behaviors.Wpf 包添加到我的项目中:
然后我将用户控件定义为:
<UserControl x:Class="Test_File_Creation.UserControlTFC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Test_File_Creation"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<TextBox>
<i:Interaction.Behaviors>
<local:RestrictStringInputBehavior />
</i:Interaction.Behaviors>
</TextBox>
</UserControl>
RestrictStringInputBehavior 函数与示例类似。
现在,在表单上我有几个文本框,我有这个定义:
xmlns:uc="clr-namespace:Test_File_Creation"
但是,我无法弄清楚如何让 UserControl 工作。 RestrictStringInputBehavior 函数永远不会被调用。我似乎没有正确调用 UserControl。
扩展 WPF 文本框以使其使用数据注释中的 MaxLength 属性的正确方法是什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。