現在visual studio2013proでWPFカスタムコントロールを使い、テキストボックスを継承した
Number型のテキストボックスを作成しています。
作成したナンバー型のテキストボックスを実際に使った時のプロパティウィンドウでText欄があるのですが、
このText欄に文字が入力された場合エラーメッセージを出してText欄を0に戻すように作っています。
以下プログラム一部抜粋
###該当のソースコード
C#
1 //グローバル変数 2 private TextBox Textbox; 3 4 //追加チェンジイベント 5 private void NumberChanged(object sender, TextChangedEventArgs e) 6 { 7 int liValue; 8 9 10 if (Textbox.IsFocused == true) 11 { 12 13 if (Textbox.Text == "" || Textbox.Text.Substring(0,1) == "0") 14 { 15 Textbox.Text = "0"; 16 } 17 else 18 { 19 liValue = Int32.Parse(Textbox.Text); 20 //if (liValue > MaxValue || liValue < MinValue || Textbox.SelectionStart == 0) 21 if (liValue > MaxValue || liValue < MinValue ) 22 { 23 Textbox.Text = gsText; 24 } 25 } 26 if (Textbox.Text == "") 27 { 28 Textbox.Text = "0"; 29 } 30 } 31 else 32 { 33 int i = 0; 34 bool result = int.TryParse(Textbox.Text, out i); 35 36 37 if (result == false) 38 { 39 MessageBox.Show("プロパティの値が不正です。", "Microsoft Visual Studio", 40 MessageBoxButton.OK, 41 MessageBoxImage.Error); 42 43 44 Text = "0"; 45 46 47 return; 48 } 49 50 liValue = Int32.Parse(Textbox.Text); 51 52 if (liValue > MaxValue || liValue < MinValue) 53 { 54 55 MessageBox.Show("プロパティの値が不正です。", "Microsoft Visual Studio", 56 MessageBoxButton.OK, 57 MessageBoxImage.Error); 58 59 Text = "0"; 60 61 } 62 63 } 64 65 } 66 67 public override void OnApplyTemplate() 68 { 69 base.OnApplyTemplate(); 70 71 this.Textbox = GetTemplateChild("Textbox") as TextBox; 72 if (this.Textbox != null) 73 { 74 this.Textbox.TextChanged += this.NumberChanged; 75 //DataObject.AddPastingHandler(Textbox, TextBoxPastingEventHandler); 76 } 77 78 } 79 80 //最大数値設定 81 [Browsable(true)] 82 [Category("NumberConfig")] 83 public int MaxValue { get; set; } 84 85 86 //最小数値設定 87 [Browsable(true)] 88 [Category("NumberConfig")] 89 public int MinValue { get; set; } 90 91XAML 92<ResourceDictionary 93 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 94 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 95 xmlns:local="clr-namespace:NumberTextBox"> 96 <Style TargetType="{x:Type local:NumberTextBox}"> 97 <Setter Property="Focusable" Value="False"/> 98 <Setter Property="IsTabStop" Value="False"/> 99 <Setter Property="Template"> 100 <Setter.Value> 101 <ControlTemplate TargetType="{x:Type local:NumberTextBox}"> 102 <TextBox x:Name="Textbox" Text="{Binding Path=Text,RelativeSource={RelativeSource TemplatedParent}}" FontWeight="Normal" TextAlignment="Right" MinWidth="120" 103 InputMethod.PreferredImeState="On" InputMethod.PreferredImeConversionMode="Alphanumeric" 104 InputMethod.IsInputMethodEnabled="False" SpellCheck.IsEnabled="True" 105 /> 106 </ControlTemplate> 107 </Setter.Value> 108 </Setter> 109 </Style> 110</ResourceDictionary>
上記のプログラムの構成は新しくテキストチェンジイベントを作成しチェンジイベントに対して
if文でフォーカス判定をします。フォーカスがない場合はIDEのプロパティウィンドウのテキストが
変更された場合の処理になっています。
このフォーカスがないパターンで実際に文字が入力された場合や、自前で作ったプロパティのMaxvalue
とMinvalueの範囲外だった場合はエラーメッセージを表示しプロパティウィンドウのテキストを0に戻しますが、
この時0に更新されたと同時にXAML側のTextも0に更新されてほしいのですが更新されません。
例としては
1.プロパティウィンドウのテキスト欄に"aaaa"と文字を入力します。
2.文字が入力されたのでプロパティウィンドウのテキスト欄が0に更新されます(デザイナの方も0に更新されます)。
3.XAMLも0に更新して欲しいが現状"aaaa"のままになってしまう。
WPFをやり始めたばかりゆえ根本的にやり方が間違っている可能性があるとは思うのですが、
問題の解決方法が分かる方がいましたらよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/11 00:32 編集
2017/01/11 01:16
2017/01/11 01:46
2017/01/11 02:00
2017/01/11 02:57
2017/01/11 03:16
2017/01/11 04:18
2017/01/11 04:46
2017/01/12 04:26
2017/01/12 05:04
2017/01/12 05:10
2017/01/12 05:42
2017/01/12 06:11
2017/01/12 06:23