teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/23 07:24

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,79 +1,79 @@
1
- 結構悩ましいですね(`double`なとこも地味にいじわる^^;
2
- takapi_csさんの回答が真っ当という気がしますが、手抜きならこんなのもあるかなと。
3
-
4
- コンバータは適当です^^; お好きな実装をどうぞ。
5
- `KeepTextBoxDisplaySynchronizedWithTextProperty`の影響範囲はよくわかりません(使ったことないので^^;
6
-
7
- ```xaml
8
- <Window
9
- x:Class="Questions295331.MainWindow"
10
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
11
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
12
- xmlns:local="clr-namespace:Questions295331"
13
- Width="800"
14
- Height="450">
15
- <StackPanel>
16
- <TextBox
17
- x:Name="textBox"
18
- Width="100"
19
- Margin="10"
20
- Text="{Binding ValidateParameter.Value, UpdateSourceTrigger=PropertyChanged}" />
21
- <Button
22
- Width="100"
23
- Command="{Binding ValidCommand}"
24
- Content="Valid"
25
- IsEnabled="{Binding (Validation.HasError), Converter={local:InverseBooleanConverter}, ElementName=textBox}" />
26
- </StackPanel>
27
- </Window>
28
- ```
29
-
30
- ```C#
31
- using System;
32
- using System.ComponentModel.DataAnnotations;
33
- using System.Diagnostics;
34
- using System.Globalization;
35
- using System.Windows;
36
- using System.Windows.Data;
37
- using System.Windows.Markup;
38
- using Reactive.Bindings;
39
-
40
- namespace Questions295331
41
- {
42
- public class InverseBooleanConverter : MarkupExtension, IValueConverter
43
- {
44
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
45
- => !(bool)value;
46
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
47
- => throw new NotImplementedException();
48
- public override object ProvideValue(IServiceProvider serviceProvider) => this;
49
- }
50
-
51
- class MainWindowsViewModel
52
- {
53
- [Range(0, 100d)]
54
- public ReactiveProperty<double> ValidateParameter { get; }
55
-
56
- public ReactiveCommand ValidCommand { get; }
57
-
58
- public MainWindowsViewModel()
59
- {
60
- ValidateParameter = new ReactiveProperty<double>()
61
- .SetValidateAttribute(() => ValidateParameter);
62
-
63
- ValidCommand = new ReactiveCommand()
64
- .WithSubscribe(() => Debug.WriteLine(ValidateParameter.Value));
65
- }
66
- }
67
-
68
- public partial class MainWindow : Window
69
- {
70
- public MainWindow()
71
- {
72
- FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
73
-
74
- InitializeComponent();
75
- DataContext = new MainWindowsViewModel();
76
- }
77
- }
78
- }
1
+ 結構悩ましいですね(`double`なとこも地味にいじわる^^;
2
+ takapi_csさんの回答が真っ当という気がしますが、手抜きならこんなのもあるかなと。
3
+
4
+ コンバータは適当です^^; お好きな実装をどうぞ。
5
+ `KeepTextBoxDisplaySynchronizedWithTextProperty`の影響範囲はよくわかりません(使ったことないので^^;
6
+
7
+ ```xml
8
+ <Window
9
+ x:Class="Questions295331.MainWindow"
10
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
11
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
12
+ xmlns:local="clr-namespace:Questions295331"
13
+ Width="800"
14
+ Height="450">
15
+ <StackPanel>
16
+ <TextBox
17
+ x:Name="textBox"
18
+ Width="100"
19
+ Margin="10"
20
+ Text="{Binding ValidateParameter.Value, UpdateSourceTrigger=PropertyChanged}" />
21
+ <Button
22
+ Width="100"
23
+ Command="{Binding ValidCommand}"
24
+ Content="Valid"
25
+ IsEnabled="{Binding (Validation.HasError), Converter={local:InverseBooleanConverter}, ElementName=textBox}" />
26
+ </StackPanel>
27
+ </Window>
28
+ ```
29
+
30
+ ```cs
31
+ using System;
32
+ using System.ComponentModel.DataAnnotations;
33
+ using System.Diagnostics;
34
+ using System.Globalization;
35
+ using System.Windows;
36
+ using System.Windows.Data;
37
+ using System.Windows.Markup;
38
+ using Reactive.Bindings;
39
+
40
+ namespace Questions295331
41
+ {
42
+ public class InverseBooleanConverter : MarkupExtension, IValueConverter
43
+ {
44
+ public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
45
+ => !(bool)value;
46
+ public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
47
+ => throw new NotImplementedException();
48
+ public override object ProvideValue(IServiceProvider serviceProvider) => this;
49
+ }
50
+
51
+ class MainWindowsViewModel
52
+ {
53
+ [Range(0, 100d)]
54
+ public ReactiveProperty<double> ValidateParameter { get; }
55
+
56
+ public ReactiveCommand ValidCommand { get; }
57
+
58
+ public MainWindowsViewModel()
59
+ {
60
+ ValidateParameter = new ReactiveProperty<double>()
61
+ .SetValidateAttribute(() => ValidateParameter);
62
+
63
+ ValidCommand = new ReactiveCommand()
64
+ .WithSubscribe(() => Debug.WriteLine(ValidateParameter.Value));
65
+ }
66
+ }
67
+
68
+ public partial class MainWindow : Window
69
+ {
70
+ public MainWindow()
71
+ {
72
+ FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
73
+
74
+ InitializeComponent();
75
+ DataContext = new MainWindowsViewModel();
76
+ }
77
+ }
78
+ }
79
79
  ```