728x90
반응형
728x170
■ TextBox 클래스의 PreviewTextInput 이벤트를 사용해 3자리마다 콤마를 갖는 숫자를 입력하는 방법을 보여준다.
▶ TextBoxExtension.cs
using System.Linq;
using System.Windows.Controls;
namespace TestProject
{
/// <summary>
/// 텍스트 박스 확장
/// </summary>
public static class TextBoxExtension
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// public
#region 숫자 카운트 구하기 - GetDigitCount(textBox)
/// <summary>
/// 숫자 카운트 구하기
/// </summary>
/// <param name="textBox">텍스트 박스</param>
/// <returns>숫자 카운트</returns>
public static int GetDigitCount(this TextBox textBox) => textBox.Text.Count(char.IsDigit);
#endregion
#region 콤마 여부 구하기 - HasAnyCommas(textBox)
/// <summary>
/// 콤마 여부 구하기
/// </summary>
/// <param name="textBox">텍스트 박스</param>
/// <returns>콤마 여부</returns>
public static bool HasAnyCommas(this TextBox textBox) => textBox.Text.Any(x => x == ',');
#endregion
}
}
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="TextBox 클래스 : PreviewTextInput 이벤트를 사용해 3자리마다 콤마를 갖는 숫자 입력하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<TextBox Name="textBox"
Width="200"
Height="25"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
MaxLength="20"
Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, StringFormat='{}{0:#,0}'}" />
</Grid>
</Window>
▶ MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 값 - Value
/// <summary>
/// 값
/// </summary>
public double Value { get; set; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
DataContext = this;
this.textBox.PreviewTextInput += textBox_PreviewTextInput;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 텍스트 박스 프리뷰 텍스트 입력시 처리하기 - textBox_PreviewTextInput(sender, e)
/// <summary>
/// 텍스트 박스 프리뷰 텍스트 입력시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
TextBox textBox = sender as TextBox;
string text = textBox.Text;
if(text.Length + 1 < 3)
{
return;
}
if((textBox.GetDigitCount()) % 3 == 0 && text.Length != 2)
{
if(!textBox.HasAnyCommas())
{
textBox.Text = text.Insert(1, ",");
}
else
{
textBox.Text = textBox.Text.Insert(textBox.Text.Length - 2, ",");
}
}
textBox.SelectionStart = textBox.Text.Length;
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] CommandManager 클래스 : AddExecutedHandler/AddCanExecuteHandler 메소드를 사용해 명령 핸들러 추가/제거하기 (0) | 2020.09.15 |
---|---|
[C#/WPF] RoutedCommand 클래스 : 커스텀 라우팅 명령 사용하기 (0) | 2020.09.15 |
[C#/WPF] Button 엘리먼트 : CommandTarget 속성 사용하기 (0) | 2020.09.15 |
[C#/WPF] KeyBinding 클래스 : 단축키 사용하기 (0) | 2020.09.15 |
[C#/WPF] Behavior<T> 클래스 : 숫자 입력 텍스트 박스 동작 사용하기 (0) | 2020.09.14 |
[C#/WPF] LogicalTreeHelper 클래스 : FindLogicalNode 정적 메소드를 사용해 엘리먼트 찾기 (0) | 2020.09.14 |
[C#/WPF] CollectionViewSource 엘리먼트 : Source 속성 사용하기 (0) | 2020.09.14 |
[C#/WPF] BindingOperations 클래스 : ClearBinding/SetBinding 정적 메소드를 사용해 바인딩하기 (0) | 2020.09.14 |
[C#/WPF] Grid 클래스 : 자동 인덱스 설정 그리드 사용하기 (0) | 2020.09.13 |
[C#/WPF] Material 메뉴 사용하기 (0) | 2020.09.13 |
댓글을 달아 주세요