티스토리 뷰
using Windows.UI.Xaml.Data;
{
public class StringFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var format = parameter as string;
if (!string.IsNullOrEmpty(format) && value is string)
{
int.TryParse(value.ToString(), out var number);
return string.Format(format, number);
}
return value;
}
{
if (value == null) return null;
int.TryParse(value.ToString().Replace(",", ""), out var number);
return number.ToString();
}
}
}
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Microsoft.Xaml.Interactivity;
{
public class NumberTextBoxBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.KeyUp += AssociatedObject_KeyUp;
}
{
switch (e.Key)
{
case VirtualKey.Number0:
case VirtualKey.Number1:
case VirtualKey.Number2:
case VirtualKey.Number3:
case VirtualKey.Number4:
case VirtualKey.Number5:
case VirtualKey.Number6:
case VirtualKey.Number7:
case VirtualKey.Number8:
case VirtualKey.Number9:
case VirtualKey.Back:
var numberText = AssociatedObject.Text;
if (string.IsNullOrEmpty(numberText)) return;
var formatString = string.Format("{0:N0}", number);
if (formatString != number.ToString())
{
AssociatedObject.Text = formatString;
AssociatedObject.SelectionStart = AssociatedObject.Text.Length;
}
break;
}
}
{
AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
base.OnDetaching();
}
}
}
MainPage.xaml
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Custom="using:Microsoft.Xaml.Interactivity"
x:Class="App1.MainPage"
xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters"
mc:Ignorable="d"
x:Name="mainPage">
<Page.Resources>
<local:StringFormatConverter x:Key="StringFormatConverter" />
</Page.Resources>
<Page.DataContext>
<local:MainPageViewModel />
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBox MinWidth="200" TextAlignment="Right"
Text="{Binding Number, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<Custom:Interaction.Behaviors>
<local:NumberTextBoxBehavior />
</Custom:Interaction.Behaviors>
</TextBox>
<TextBlock Text="{Binding Number}" />
<Button Click="ButtonBase_OnClick" />
</StackPanel>
</Grid>
</Page>
결과 화면입니다.
파일
'UWP & Windows App > Beginner' 카테고리의 다른 글
VSTS를 이용한 프로젝트 관리 part1 (0) | 2017.12.26 |
---|---|
Fall Creators Update에서 VisualState와 Storyboard 않되는 현상 해결하기 (0) | 2017.12.13 |
.Net Standard가 뭐에요? (2) | 2017.11.07 |
내급여 UWP 앱 개발 part6 (0) | 2017.09.25 |
내급여 UWP 앱 개발 part5 (0) | 2017.09.18 |
- Total
- Today
- Yesterday
- #uwp
- ef core
- C#
- PRISM
- Always Encrypted
- .net 5.0
- IOT
- ComboBox
- XAML
- windows 11
- dotNETconf
- kiosk
- MVVM
- .net
- uno-platform
- uno platform
- UWP
- LINQ
- Microsoft
- #Windows Template Studio
- Visual Studio 2022
- Build 2016
- WPF
- Bot Framework
- #MVVM
- Behavior
- visual studio 2019
- Cross-platform
- Windows 10
- #prism
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |