How can I stop the UI layer from performing its conversion validation, or at least have it continue on? If I have a textbox bound to a DateTime:
// view
<TextBox x:Name="StartTimeTextBox">
<TextBox.Text>
<Binding Path="StartTime"
StringFormat="t"
NotifyOnValidationError="True"
ValidatesOnDataErrors="True"
ValidatesOnExceptions="True" >
<Binding.ValidationRules>
<va:AlwaysSucceedsValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
// view model
[MyValidationAttribute]
public DateTime StartTime {get; set;}
When the user selects all the text text in the textbox and deletes it (or types in "asdf"), the conversion fails, it gets a red border and validation stops. I've tried using ValidatesOn... attributes (which I thought would allow my "MyValidationAttribute" to execute) without success. I've also tried adding a ValidationRule that always returns true no matter what the Text - but nothing works.
If you bind TextBox.Text to a DateTime, and the user types in "my hovercraft is full of little lambs", what can the Binding possibly assign to your viewmodel property for you to validate? There's nothing it can do.
You can set Validation.ErrorTemplate for the TextBox to an empty template, and that'll get rid of the red outline business, but you still won't get anything validatable in your viewmodel property.
If you want to validate string input from the user as a valid or invalid date, you're going to have to do that at some point where you have the raw string input in your hands.
If you want to do it in your viewmodel, that means giving your viewmodel a string property for StartTime, and binding that to the TextBox. Call it StringStartTime; in its setter, if the string is valid it sets DateTime StartTime; if not valid, it leaves StartTime alone but sets some error property, or throws an exception, or whatever.
Related
I have a TextBox displaying the time part of a DateTime:
<TextBox HorizontalAlignment="Left" Height="23" Margin="0,13,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Validation.Error="Validation_OnError">
<TextBox.Text>
<Binding Path="MyDate" StringFormat="HH:mm" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<c:TimeValidator></c:TimeValidator>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Is it possible to do the validation on property change, and the conversion on lost focus?
I want to have the validation on property changed, but I want to have my data source updated on lost focus. Otherwise, the converter will kick in while the user is editing in the TextBox. This might be a problem if the values is 10:50 and the user deletes the last number, so that the value becomes 10:5. The converter will then convert this to 10:50. This is okay to do on lost focus, but not on property changed. But for the sake of the validator, i want to validate on property change so the user have the red border as long as the entered value is not valid.
Yes! I was just wrestling with this. AFAIK, there is no XAML combination for this--it must be done in the codebehind, and you need a direct reference to the element.
Element.GetBindingExpression(PropertyName).ValidateWithoutUpdate();
You'll probably want to check that GetBindingExpression doesn't return null; this will run any converters you have attached (presumably to supply the converted value to converters with ValidationStep set to ConvertedProposedValue), but will not update the source. And, of course, you'll have to call this in some event, perhaps TextChanged or somesuch. Here is the MSDN documentation for it: https://msdn.microsoft.com/en-us/library/system.windows.data.bindingexpressionbase.validatewithoutupdate(v=vs.110).aspx
Use this code:
BindingExpression expression =
txtStudentName.GetBindingExpression(TextBox.TextProperty);
expression.ValidateWithoutUpdate();
If you want to update it's source after check use this code:
BindingExpression expression =
txtStudentName.GetBindingExpression(TextBox.TextProperty);
expression.ValidateWithoutUpdate();
if (expression!=null && !expression.HasError)
expression.UpdateSource();
I have a ValidationRules on a textbox;
<TextBox Margin="5,5,5,0" Name="myTextBox" >
<Binding Path="myID" NotifyOnValidationError="True" ValidatesOnDataErrors="True" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<local:ValueCannotBlankValidator ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
Now this works if the user changes the value in the textbox. The problems that it doesn't fire on load. Figured it would be a simple fix of changing UpdateSourceTrigger="PropertyChanged" to UpdateSourceTrigger="LostFocus" but that causes the ValidationRules not to fire. Thanks for the help.
If you set UpdateSourceTrigger="LostFocus", the validation happens when input focus is set to another control, on the other hand, UpdateSourceTrigger="PropertyChanged" fires every time the text is changed, acts much like a TextBox's TextChanged event.
ValidatesOnTargetUpdated="True" ensures that the text is validated on load, your XAML code is correct. If you set a breakpoint in ValueCannotBlankValidator.Validate method you would probably find it is actually fired on load.
I doubt your validator returns a valid result at the first validation, at that moment the Text property of the TextBox is null, if you compare null against string.Empty (""), you get an incorrect result.
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
ValidationResult trueResult = new ValidationResult(true, "not blank");
string str = value as string; //value is null on load, don't compare it against ""
if (string.IsNullOrEmpty(str))
return new ValidationResult(false, "blank");
else
return trueResult;
}
I have the a bunch of textboxes all with data validation as follows:
xaml
<TextBox>
<TextBox.Text>
<Binding Path="Name" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:Validation2/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
c#
public class Validation2 : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
double result;
return double.TryParse(value.ToString(), out result) == true ? new ValidationResult(true, null) : new ValidationResult(false, "error");
}
}
This works nicely, whenever I put anything but a number in the textboxes an error pops up. Now I have a button to send the "form", I'd like the button to check if there were any validation errors before doing anything. How would I go about doing that.
Validation occurs before applying new value to source property. In your case - when you change property. In wpf there are also few more cases, but there is no OnFormClosing or similar. It's by design: control property may be bound to other control property (or several controls bound to same property), so validation occurs at latest when you change focus.
If you don't have cross-bindings, one property is bound to only one control, then you may utilize UpdateSourceTrigger.Explicit - call UpdateSource() for each binding when form is about to be closed.
Other solution would be to don't display errors as popups. Error status could be a red border or ! icon near.
I myself don't use validation mechanism at all. Instead, I have self-validating controls, to example, TextBox with property IsDouble to enable automatic validation for double values and property GetDouble, to parse value. I like more to validate everything at the end, while displaying actual status if validation will be ok or not to the user (red border, flashing caption, etc. per control).
I have a dialog in my project that the user enters some values in and when he hits OK I add an item to my database. I am using Entity Framework, so my adding to database code is something like this:
TransactionItem _item = new TransactionItem();
_item.DoctorID = (int)cmbDoctor.SelectedValue;
_item.TransactionCategoryID = (int)_dlg.cmbCat.SelectedValue;
_item.TransactionMethodID = (int)_dlg.cmbMethod.SelectedValue;
_item.Amount = int.Parse(_dlg.txtAmount.Text);
_item.DocumentID = _dlg.txtDocNum.Text;
_item.Info = _dlg.txtInfo.Text;
_item.Date = _dlg.dteDate.SelectedDate.ToString();
_db.TransactionItems.Add(_item);
_db.SaveChanges();
But the problem is there is nothing to bind and enable validating. I have tried making an empty object in my window and bind text box to it, but it had its own problems and didn't work as expected. I just want to when users enter values or when he hits OK, check if all of fields are valid (for example one of problems was if the user didn't enter any value, it is still valid even though the stringnotnull validator is enabled, but the most important problem was that it automatically set the textbox's text to null and mark it as a null value).
And I have made my own validator and here is a example of how I implemented them on one of my textboxes:
<TextBox Name="txtAmount" HorizontalAlignment="Left" Height="23" Margin="83,169,0,0" VerticalAlignment="Top" Width="224" Tag="T">
<TextBox.Text>
<Binding Path="myitem" ElementName="myWindow" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<Validators:StringNullValidationRule/>
<Validators:IsNumericValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Why don't you create a property in your viewmodel for each value the user needs to enter, and bind to it? Then you could use these properties when adding an item. For example:
ViewModel:
public int Amount { get; set; }
...
public void AddItem()
{
TransactionItem _item = new TransactionItem();
// ...
_item.Amount = Amount;
}
XAML:
<TextBox Name="txtAmount" HorizontalAlignment="Left" Height="23" Margin="83,169,0,0" VerticalAlignment="Top" Width="224" Tag="T">
<TextBox.Text>
<Binding Path="DataContext.Amount" ElementName="myWindow">
<Binding.ValidationRules>
<Validators:StringNullValidationRule/>
<Validators:IsNumericValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
I also recommend having a look at the INotifyDataErrorInfo interface (or the IDataErrorInfo interface if you're using .NET 4.0 or lower) to implement validations.
Use the IDataErrorInfo interface. You can implement it in your ViewModel or your Model class depending on your design. An example of how you can do it is in WPF: Validation made easy with IDataErrorInfo.
And I recommend you read this great Josh Smith article: WPF Apps With The Model-View-ViewModel Design Pattern. There you can see a good example of validation.
I am having trouble getting a custom ValidationRule to fire, when it is associated with an Expander.Header binding. In fact, the only place I can seem to get these custom rules to fire is in a DataGrid.RowValidationRules block...
The expander is defined in my Window XAML file like so;
<Expander Style="{StaticResource ValidatedSecondLevelExpanderStyle}">
<Expander.Header>
<Binding Path="Name" Mode="OneWay" ValidatesOnDataErrors="True" NotifyOnValidationError="True">
<Binding.ValidationRules>
<ValidationRules:BoundObjectIsValid />
</Binding.ValidationRules>
</Binding>
</Expander.Header>
</Expander>
The bound property 'Name' is displayed correctly, but the validation rule 'BoundObjectIsValid' does not get invoked. Is this possible, and if so, what am I missing?
I know that I could alternately implement IDataErrorInfo on the bound object, however the object can't sensibly validate itself without some context that is provided by other parts of the system. Refactoring is possible, but I'd love to get the ValidationRules to work first!
Refer to the msdn.
The binding engine checks each ValidationRule that is associated with a binding every time it transfers an input value, which is the binding target property value, to the binding source property.
So here in your case, you don't have an inpurt value being transfered to the source property since your Expander.header is not a control which you can use to input values.
Edit: But there is a property named ValidatesOnTargetUpdated' in the ValidationRule. When setting it to true, the validationrule will be applied when the target property is updated