code_junkie

How do I get rid of the red rectangle when my wpf binding validation has failed and the containing panel is no longer visible?

Nov 12th, 2011
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. <Window x:Class="WpfDataBindingProblem.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Window1" Height="300" Width="300">
  5. <StackPanel Margin="5">
  6.  
  7. <StackPanel Name="panel1" Visibility="Visible" Margin="5">
  8. <TextBox Name="DataBoundTextBox">
  9. <Binding Path="TextValue">
  10. <Binding.ValidationRules>
  11. <ExceptionValidationRule/>
  12. </Binding.ValidationRules>
  13. </Binding>
  14. </TextBox>
  15. </StackPanel>
  16.  
  17. <StackPanel Name="panel2" Visibility="Collapsed" Margin="5">
  18. <TextBlock>
  19. The quick brown fox jumps over the lazy dog.
  20. </TextBlock>
  21. </StackPanel>
  22.  
  23. <Button Click="Button_Click" Margin="5">
  24. Toggle panels
  25. </Button>
  26.  
  27. </StackPanel>
  28. </Window>
  29.  
  30. using System;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. using System.Linq;
  34. using System.Text;
  35. using System.Windows;
  36. using System.Windows.Controls;
  37. using System.Windows.Data;
  38. using System.Windows.Documents;
  39. using System.Windows.Input;
  40. using System.Windows.Media;
  41. using System.Windows.Media.Imaging;
  42. using System.Windows.Navigation;
  43. using System.Windows.Shapes;
  44.  
  45. namespace WpfDataBindingProblem {
  46.  
  47. public partial class Window1 : Window {
  48. public Window1() {
  49. InitializeComponent();
  50.  
  51. this.DataContext = new MyClass("default");
  52. }
  53.  
  54. private void Button_Click(object sender, RoutedEventArgs e) {
  55. panel1.Visibility = panel1.Visibility == Visibility.Collapsed ?
  56. Visibility.Visible : Visibility.Collapsed;
  57. panel2.Visibility = panel2.Visibility == Visibility.Collapsed ?
  58. Visibility.Visible : Visibility.Collapsed;
  59. }
  60. }
  61.  
  62. public class MyClass : INotifyPropertyChanged {
  63.  
  64. private string mTextValue;
  65.  
  66. public MyClass(string defaultText) {
  67. TextValue = defaultText;
  68. }
  69.  
  70. public string TextValue {
  71. get {
  72. return mTextValue;
  73. }
  74. set {
  75. mTextValue = value;
  76. if (string.IsNullOrEmpty(mTextValue)) {
  77. throw new ApplicationException("Text value cannot be empty");
  78. }
  79. OnPropertyChanged(new PropertyChangedEventArgs("TextValue"));
  80. }
  81. }
  82.  
  83. public event PropertyChangedEventHandler PropertyChanged;
  84.  
  85. protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
  86. if (this.PropertyChanged != null) {
  87. this.PropertyChanged(this, e);
  88. }
  89. }
  90. }
  91.  
  92. }
  93.  
  94. <Setter Property="Validation.ErrorTemplate">
  95. <Setter.Value>
  96. <ControlTemplate>
  97. <ControlTemplate.Resources>
  98. <BooleanToVisibilityConverter x:Key="converter" />
  99. </ControlTemplate.Resources>
  100. <DockPanel LastChildFill="True">
  101. <Border
  102. BorderThickness="1"
  103. BorderBrush="Red"
  104. Visibility="{Binding ElementName=placeholder, Mode=OneWay, Path=AdornedElement.IsVisible, Converter={StaticResource converter}}">
  105. <AdornedElementPlaceholder x:Name="placeholder" />
  106. </Border>
  107. </DockPanel>
  108. </ControlTemplate>
  109. </Setter.Value>
  110. </Setter>
  111.  
  112. private void Button_Click(object sender, RoutedEventArgs e) {
  113. if (panel1.Visibility == Visibility.Collapsed) {
  114. panel1.Visibility = Visibility.Visible;
  115. DataBoundTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
  116. panel2.Visibility = Visibility.Collapsed;
  117. }
  118. else {
  119. panel1.Visibility = Visibility.Collapsed;
  120. DataBoundTextBox.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
  121. panel2.Visibility = Visibility.Visible;
  122. }
  123. }
Add Comment
Please, Sign In to add comment