简体   繁体   English

WPF TextBox MaxLength 属性在应用某些样式后不起作用

[英]WPF TextBox MaxLength Property does not work after applying some style

I have an issue in my C# code.我的 C# 代码有问题。 Could you please help me?请你帮助我好吗? I have a textbox with such XAML code:我有一个带有这样 XAML 代码的文本框:

<TextBox 
        Margin="0,10" 
        x:Name="SomeName"
        TextAlignment="Center"
        FontWeight="Bold"
        Focusable="True"
        MaxLength="100"
        Width="200"
        Style="{StaticResource SomeStyle}"
        Text="{Binding PropName, UpdateSourceTrigger=PropertyChanged}" />

UPDATE更新

Here is my SomeStyle code:这是我的 SomeStyle 代码:

    <Style x:Key="SomeStyle" TargetType="{x:Type TextBox}" >
    <Setter Property="Focusable" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" 
                        Background="{TemplateBinding Background}" >
                    <Grid ClipToBounds="True" >
                        <TextBox Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, 
                                                Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                 Panel.ZIndex="2" />

                        <TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}">
                                    <Setter Property="FontStyle" Value="Italic" />
                                    <Setter Property="BorderThickness" Value="0" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
                                        </DataTrigger>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="BorderThickness" Value="0" />
                                            <Setter Property="BorderBrush" Value="Transparent" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <!--<Setter Property="BorderBrush" Value="Transparent" />-->
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

If I remove a Style prop, the MaxLength prop work properly.如果我删除 Style 道具,MaxLength 道具会正常工作。 This referenced style does not contain a constraint for MaxLength.此引用样式不包含 MaxLength 的约束。 Any ideas?有任何想法吗?

You have others TextBoxes in your TextBox Control Template and you don't pass them MaxLength value, so it doesn't work.您的文本框控件模板中有其他文本框,并且您没有向它们传递 MaxLength 值,因此它不起作用。 What you are trying to do with this template?你想用这个模板做什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM