私のデータオブジェクトにバインドされたテキストボックスがあります。検証に失敗した場合は、エラーメッセージを含むポップアップを表示したいと思います。 XAMLではうまく動作します。私は、次のXAMLを使用しています:コード内のPopup.IsOpenをValidation.HasErrorにバインドする方法
<TextBox Height="23" Margin="54,12,104,0" Name="textBox1"
VerticalAlignment="Top" Text="{Binding Value, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Popup Name="myPopup" PlacementTarget="{Binding ElementName=textBox1}"
IsOpen="{Binding ElementName=textBox1, Path=(Validation.HasError), Mode=OneWay}"
>
<TextBlock Name="myPopupText" Background="LightBlue" Foreground="Blue">
The value is invalid
</TextBlock>
</Popup>
私の問題は、私は、ポップアップを作成する必要があり、コードに結合し、私はそれが仕事を得ることができないということです。私はいくつかの異なるオプションを試しました。私はバインディングが全く機能するかどうかを見るためにダミーコンバータも使用しました。それは私がそれを(それは初期値を取得する)を作成するときにバインディングが動作するようだが、その後何も起こりません。 Validation.HasErrorが正しく更新されていることがわかります(TextBoxの枠線が赤く変わります)が、それだけです。私のダミーコンバータは呼び出されません。以下は、私が使用しているコードです:
Popup popup = new Popup();
popup.Name = "somepopup";
// Source is the textbox which is bound to the data object
popup.PlacementTarget = source;
popup.Placement = PlacementMode.Bottom;
TextBlock txtblock = new TextBlock();
txtblock.Background = Brushes.LightBlue;
txtblock.Foreground = Brushes.Blue;
txtblock.Text = "this is the error message";
popup.Child = txtblock;
Binding is_open_binding = new Binding("(Validation.HasError)");
is_open_binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
is_open_binding.Source = source;
is_open_binding.Mode = BindingMode.OneWay;
is_open_binding.NotifyOnValidationError = true;
is_open_binding.ValidatesOnExceptions = true;
is_open_binding.Converter = new TempValueConverter();
popup.SetBinding(Popup.IsOpenProperty, is_open_binding);