【问题标题】:WPF TextBox MaxLength Property does not work after applying some styleWPF TextBox MaxLength 属性在应用某些样式后不起作用
【发布时间】:2021-01-15 16:39:41
【问题描述】:

我的 C# 代码有问题。请你帮助我好吗?我有一个带有此类 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}" />

更新

这是我的 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>

如果我删除 Style 道具,MaxLength 道具会正常工作。此引用样式不包含 MaxLength 的约束。有什么想法吗?

【问题讨论】:

  • 请展示你的风格
  • @Lana,已更新

标签: c# .net wpf xaml textbox


【解决方案1】:

您的文本框控件模板中有其他文本框,并且您没有向它们传递 MaxLength 值,因此它不起作用。你想用这个模板做什么?

【讨论】:

  • 它也用于其他文本框,我不需要那个约束
  • 没关系,他们会得到默认值并忽略它。您应该通过 TemplateBinding 传递此属性
  • 将它作为 TemplateBinding 传递对我有帮助。非常感谢!!!
猜你喜欢
  • 2011-06-15
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
相关资源
最近更新 更多