扩展WPF C#中的文本框以使用MaxLength

扩展WPF C#中的文本框以使用MaxLength,c#,wpf,user-controls,C#,Wpf,User Controls,我正在WPF项目中尝试这样做: 也就是说,根据该字段的数据注释扩展文本框,使其具有默认的MaxLength 我已将包Microsoft.Xaml.Behaviors.Wpf添加到我的项目中: 然后,我将用户控件定义为: <UserControl x:Class="Test_File_Creation.UserControlTFC" xmlns="http://schemas.microsoft.com/winfx/2006/xaml

我正在WPF项目中尝试这样做:

也就是说,根据该字段的数据注释扩展文本框,使其具有默认的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>
但是,我不知道如何让UserControl工作。从未调用RestrictStringInputBehavior函数。我似乎没有正确地调用UserControl


扩展WPF文本框以使其使用数据批注中的MaxLength属性的正确方法是什么?

您不需要
用户控件
,只需使用
文本框
控件即可。另外,您需要将
文本框的
文本
属性绑定到具有长度属性的某个属性(
[必需,StringLength(50)]
)是的,我理解这一点。我在字段上已经有了[MaxLength(100)]等(而不是StringLength()),并且我已经有了这个绑定:。如果已经在数据注释中定义了MaxLength,我希望避免在其上添加MaxLength。
xmlns:uc="clr-namespace:Test_File_Creation"