دسته بندی ها
02:03 1398/12/25

جدا کردن سه رقم سه رقم اعداد در TextBox در WPF

مشخصات سوال کننده :
کاربر : pedram_khan     امتیاز کاربر : 277     رتبه کاربر : 6
دسته بندی : WPF سی شارپ

به این سوال امتیاز بدهید    0
تعداد بازدید این سوال : 4623
هوش مصنوعی

پاسخ هوش مصنوعی

برای جداسازی اعداد سه رقم سه رقم در یک TextBox در WPF، می توانید از یک رفتار (Behavior) استفاده کنید تا تغییرات در TextBox را مدیریت کنید. در اینجا یک مثال برای این کار ارایه می شود:

using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace YourNamespace
{
    public class StringFormatBehavior
    {
        public static readonly DependencyProperty FormattedTextProperty =
            DependencyProperty.RegisterAttached("FormattedText", typeof(string), typeof(StringFormatBehavior), new PropertyMetadata(string.Empty, OnFormattedTextChanged));

        public static string GetFormattedText(DependencyObject obj)
        {
            return (string)obj.GetValue(FormattedTextProperty);
        }

        public static void SetFormattedText(DependencyObject obj, string value)
        {
            obj.SetValue(FormattedTextProperty, value);
        }

        private static void OnFormattedTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is TextBox textBox)
            {
                textBox.TextChanged -= TextBox_TextChanged;
                textBox.Text = FormatNumber(textBox.Text);
                textBox.TextChanged += TextBox_TextChanged;
            }
        }

        private static void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (sender is TextBox textBox)
            {
                textBox.TextChanged -= TextBox_TextChanged;
                string formattedText = FormatNumber(textBox.Text);
                textBox.Text = formattedText;
                textBox.CaretIndex = formattedText.Length; // Move caret to end
                textBox.TextChanged += TextBox_TextChanged;
            }
        }

        private static string FormatNumber(string input)
        {
            if (string.IsNullOrEmpty(input))
                return input;

            string digitsOnly = Regex.Replace(input, @"[^\d]", string.Empty);
            return string.Format(CultureInfo.InvariantCulture, "{0:N0}", long.Parse(digitsOnly));
        }
    }
}

سپس می توانید از این Behavior در XAML استفاده کنید:

<TextBox Width="200" Height="30" local:StringFormatBehavior.FormattedText="{Binding YourNumberProperty}" />

این کد به کاربران اجازه می دهد تا اعداد را در TextBox وارد کنند و به صورت خودکار سه رقم سه رقم جدا شوند. توجه داشته باشید که باید namespace مربوط به behavior را در XAML اضافه کنید.

به این پاسخ امتیاز بدهید    0
امتیاز: - رتبه: -
پاسخ دهنده : Sowgandi 11:49 1399/01/10

با سلام و خسته نباشید.

ابتدا خاصیت MaxLength تکست باکس رو عدد 20 قرار بدید و در رویداد TextChanged تکست باکس خود کد زیر رو وارد کنید.

 try
            {
                if (MyTextBox.Text != string.Empty)
                {
                    MyTextBox.Text = string.Format("{0:N0}", double.Parse(MyTextBox.Text.Replace(",", "")));
                    MyTextBox.Select(MyTextBox.MaxLength, 0);
                }
            }
            catch ()
            {
                return;
            }

 

به این پاسخ امتیاز بدهید    0
امتیاز: 876 رتبه: 3
برای ارسال پاسخ لطفا وارد حساب کاربری خود شوید.   ورود