简体   繁体   中英

How can I limit the amount of number characters in WPF c#

I actually have two questions so Ill start with the first: I am getting ready for a project I'm working on and seeing if I can limit the range of characters in the text box field. It has to all be numbers and needs to be eight characters. no more no less. Is there any way I can do that. Because it does not like it when I use the.length line and the code I used when ran will not display the error message. Here is my attempt: (the GUI isn't complex. it has a text box to type in an ID. two text blocks being the result and error message. with a button saying submit.) and My second question is how do I store the ID and display it?

    public partial class MainWindow : Window
{
    int range;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void sub_Click(object sender, RoutedEventArgs e)
    {
        range = int.Parse(Type.Text);

        while (range>0 || range < 8)
        {
            Error.Text = "ID must be eight characters long.";
        }
    }
}

I create a simple demo for your one need: 在此处输入图像描述

You could create the Textbox in the UI with Validation and MaxLength:

<TextBox Name="txtMyLength"
             MaxLength="8"
             Width="200"
             Margin="140"
             HorizontalAlignment="Left"
             VerticalAlignment="Center"
             Text="{Binding validModel.MyLength, ValidatesOnDataErrors=True}" />

Then set the Style for the TextBox for error tip:

<Style TargetType="TextBox">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border BorderThickness="1" BorderBrush="Red" VerticalAlignment="Top">
                            <Grid>
                                <AdornedElementPlaceholder x:Name="adorner" Margin="-1"/>
                            </Grid>
                        </Border>
                        <Border x:Name="errorBorder" Background="Red" Margin="8,0,0,0"
                            Opacity="0" CornerRadius="0"
                            IsHitTestVisible="False"
                            MinHeight="24" >
                            <TextBlock Text="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                                   Foreground="Black" Margin="8,3,8,3" TextWrapping="Wrap" VerticalAlignment="Center"/>
                        </Border>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                ......
                            </DataTrigger.Binding>
                            <DataTrigger.EnterActions>
                                <BeginStoryboard x:Name="in">
                                    <Storyboard>
                                      ......
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                            <DataTrigger.ExitActions>
                                <StopStoryboard BeginStoryboardName="in"/>
                                <BeginStoryboard x:Name="out">
                                    .......
                                </BeginStoryboard>
                            </DataTrigger.ExitActions>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

create the condition in the ValidModel like

public string this[string columnName]
        {
        get
        {
            int txtLength = 0;
            string result = string.Empty;
            if (MyLength != "" && MyLength != null)
            {
                txtLength = MyLength.ToString().Length;
                                }
            switch (columnName)
            {
                case "MyLength": if (txtLength != 8 ) result = "ID be 8 numbers"; break;
                case .......
            };
            return result;
        }

You can select the Maximu Length property for any TextBox:

YourTextBox.MaxLength = 50;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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