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
| public class TextBoxValidationHelper
{
#region UpdateRule (ATTACHED)
public static bool GetUpdateRule(DependencyObject obj) { return (bool)obj.GetValue(UpdateRuleProperty); }
public static void SetUpdateRule(DependencyObject obj, bool value) { obj.SetValue(UpdateRuleProperty, value); }
// Using a DependencyProperty as the backing store for UpdateRule. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UpdateRuleProperty =
DependencyProperty.RegisterAttached(
"UpdateRule",
typeof(bool),
typeof(TextBoxValidationHelper),
// new PropertyMetadata(false)
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(UpdateRuleChanged))
// new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender , new PropertyChangedCallbackUpdateRuleChanged))
);
private static void UpdateRuleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBox control = d as TextBox;
//control.Loaded += (ss, ee) =>
//{
// BindingExpression be = control.GetBindingExpression(TextBox.TextProperty);
// if (be != null)
// {
// be.UpdateSource();
// }
//};
control.IsVisibleChanged += (ss, ee) =>
{
BindingExpression be = control.GetBindingExpression(TextBox.TextProperty);
if (be != null)
{
be.UpdateSource();
}
};
}
#endregion
} |
Partager