Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Anurag,</p> <p>There are a lot of assumptions I have to make here, but I'll give it a shot.</p> <p>You probably have something like this...</p> <pre><code>// ... public static string GetValue(Dependency obj) { // ... } // ... public static void SetValue(DependencyObject obj, string value) { // ... } // Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc... public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached("Value", typeof(string), typeof(MyCustomControl), new UIPropertyMetadata(OnValuePropertyChanged)); public static void OnValuePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { string newValue = e.NewValue as string; // You validate your value here, // Then if fails, revert to old value. if(!SomeCondtion(newValue)) SetValue(obj,e.OldValue as string); } </code></pre> <p>This is definitely not the best approach to validate data. There are other, more efficient approaches that will give you more flexibility.</p> <ol> <li>Apply a ValidationRule on your TexBox. This is my first recommendation because it will give you control on displaying the error when validation fails. If you're after a simple validation, check out my <a href="http://www.codeproject.com/KB/WPF/wpfsimplevalidator.aspx" rel="nofollow noreferrer">article</a>.</li> <li>Listen for the TextBox.TextChanged event. This is cumbersome and is not re-usable at most hacked.</li> <li><p>Don't use DependecyPropertyChanged callback method. Use a registered string Property and in your setter, apply logic, e.g.</p> <pre><code>public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(UserControlWithTextBox)); private string _oldValue; public string Value { get { return GetValue(ValueProperty) as string; } set { // Check if the value passes your validation logic if(SomeCondtion(value)) { // If yes, then set the value, and record down the old value. SetValue(ValueProperty, value); _oldValue = value; } else { // Else logic fails, set the value to old value. SetValue(ValueProperty, _oldValue); } // Implement INotifyPropertyChanged to update the TextBox. if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Value")); } } </code></pre></li> </ol> <p>Once again, as you can see from my answer that you can still show code in your question to help others answer your question no matter how complex your solution may be (if you want a good answer, you need to put in the effort to ask a good question). My answer may not work for you but maybe it might give you some "Googling" directions. Hope it helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload