1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public class TextBoxEx : TextBox
{
public TextBoxEx()
{
}
Border border = null;
ToolTip toolTip = null;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Grid grid = this.GetTemplateChild("RootElement") as Grid;
this.border = grid.FindName("ValidationErrorElement") as Border;
this.toolTip = this.border.GetValue(ToolTipService.ToolTipProperty) as ToolTip;
string value = this.ErrorMessage;
if (value != null)
this.ErrorMessageChange(value);
ControlTemplate template = this.ErrorTemplate;
if (template != null)
this.ErrorTemplateChange(template);
}
/// <summary>
/// Changement de message
/// </summary>
public string ErrorMessage
{
get { return (string)GetValue(ErrorMessageProperty); }
set { SetValue(ErrorMessageProperty, value); }
}
// Using a DependencyProperty as the backing store for ErrorMessage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ErrorMessageProperty =
DependencyProperty.Register("ErrorMessage", typeof(string), typeof(TextBoxEx), new PropertyMetadata(null,OnErrorMessageChange));
/// <summary>
/// Changement de Message
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private static void OnErrorMessageChange(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
TextBoxEx tbx = sender as TextBoxEx;
tbx.ErrorMessageChange((string)args.NewValue);
}
/// <summary>
/// Changement de message d'erreur
/// </summary>
/// <param name="errorMessage"></param>
private void ErrorMessageChange(string errorMessage)
{
if (this.border == null)
return;
if (errorMessage != null)
{
this.toolTip.Content = errorMessage;
this.border.Visibility = Visibility.Visible;
}
else
{
this.border.Visibility = Visibility.Collapsed;
}
}
/// <summary>
/// Changement de l'erreur de Template
/// </summary>
public ControlTemplate ErrorTemplate
{
get { return (ControlTemplate)GetValue(ErrorTemplateProperty); }
set { SetValue(ErrorTemplateProperty, value); }
}
// Using a DependencyProperty as the backing store for ErrorTemplate. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ErrorTemplateProperty =
DependencyProperty.Register("ErrorTemplate", typeof(ControlTemplate), typeof(TextBoxEx), new PropertyMetadata(null, OnErrorTemplateChange));
/// <summary>
/// Changement de Template
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private static void OnErrorTemplateChange(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
TextBoxEx tbx = sender as TextBoxEx;
tbx.ErrorTemplateChange((ControlTemplate)args.NewValue);
}
/// <summary>
/// Changement de message d'erreur
/// </summary>
/// <param name="errorMessage"></param>
private void ErrorTemplateChange(ControlTemplate errorTemplate)
{
if (this.toolTip == null)
return;
this.toolTip.Template = errorTemplate;
}
}
} |
Partager