回答編集履歴

1

追記

2019/05/01 06:27

投稿

Zuishin
Zuishin

スコア28669

test CHANGED
@@ -3,3 +3,165 @@
3
3
 
4
4
 
5
5
  クラスの方でプロパティのセッターを工夫し、何か値が入れられたらそれを検証してプロパティの値を調整するようにしてください。
6
+
7
+
8
+
9
+ # 追記
10
+
11
+
12
+
13
+ XAML
14
+
15
+ ```C#
16
+
17
+ <Window x:Class="WpfApp1.MainWindow"
18
+
19
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
20
+
21
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
22
+
23
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
24
+
25
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
26
+
27
+ xmlns:local="clr-namespace:WpfApp1"
28
+
29
+ mc:Ignorable="d"
30
+
31
+ Title="MainWindow" Height="450" Width="800">
32
+
33
+ <Window.DataContext>
34
+
35
+ <local:ViewModel/>
36
+
37
+ </Window.DataContext>
38
+
39
+ <Grid>
40
+
41
+ <Grid.RowDefinitions>
42
+
43
+ <RowDefinition Height="Auto"/>
44
+
45
+ <RowDefinition/>
46
+
47
+ </Grid.RowDefinitions>
48
+
49
+ <Grid.ColumnDefinitions>
50
+
51
+ <ColumnDefinition/>
52
+
53
+ <ColumnDefinition Width="Auto"/>
54
+
55
+ </Grid.ColumnDefinitions>
56
+
57
+ <TextBox Grid.Row="0" Grid.Column="0" Text="{Binding TextValue}"/>
58
+
59
+ <Button Grid.Row="0" Grid.Column="1">Click</Button>
60
+
61
+ </Grid>
62
+
63
+ </Window>
64
+
65
+ ```
66
+
67
+
68
+
69
+ ViewModel.cs
70
+
71
+ ```C#
72
+
73
+ using System;
74
+
75
+ using System.Collections.Generic;
76
+
77
+ using System.ComponentModel;
78
+
79
+ using System.Runtime.CompilerServices;
80
+
81
+
82
+
83
+ namespace WpfApp1
84
+
85
+ {
86
+
87
+ public class ViewModel : INotifyPropertyChanged
88
+
89
+ {
90
+
91
+ #region INotifyPropertyChanged
92
+
93
+ public event PropertyChangedEventHandler PropertyChanged;
94
+
95
+ protected virtual void OnPropertyChanged(string propertyName)
96
+
97
+ {
98
+
99
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
100
+
101
+ }
102
+
103
+ protected bool SetProperty<T>(ref T field, T value, [CallerMemberName]string propertyName = null)
104
+
105
+ {
106
+
107
+ if (EqualityComparer<T>.Default.Equals(field, value)) return false;
108
+
109
+ field = value;
110
+
111
+ OnPropertyChanged(propertyName);
112
+
113
+ return true;
114
+
115
+ }
116
+
117
+ #endregion
118
+
119
+
120
+
121
+ private string textValue;
122
+
123
+ public string TextValue
124
+
125
+ {
126
+
127
+ get => textValue;
128
+
129
+ set
130
+
131
+ {
132
+
133
+ if (DateTime.TryParse(value, out DateTime dateTime))
134
+
135
+ {
136
+
137
+ value = dateTime.ToString("yyyy/MM/dd");
138
+
139
+ }
140
+
141
+ else if (int.TryParse(value, out int i))
142
+
143
+ {
144
+
145
+ value = i.ToString("N0");
146
+
147
+ }
148
+
149
+ else if (double.TryParse(value, out double d))
150
+
151
+ {
152
+
153
+ value = d.ToString("N2");
154
+
155
+ }
156
+
157
+ SetProperty(ref textValue, value);
158
+
159
+ }
160
+
161
+ }
162
+
163
+ }
164
+
165
+ }
166
+
167
+ ```