I have three text boxes on my wpf application. When the user enters the value in these text boxes and clicks on Add
, these are added to the list and displayed in data grid.
The text boxes are not bound to anything as such and I add these text box values to an observale collection bound to data grid. I want to prevent the user to enter empty values in text boxes. How is that done?
I saw some examples but those all had the text box boudn to values and then used Binding.Validation
. How will this be done in my case when there's binding to text box?
I also have a button which has to be freezed when the empty values are being entered. for that, I sed the following approach by making a class and binding the class in the following manner;
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="false" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=textBox1, Path=(Validation.HasError)}" Value="false" />
<Condition Binding="{Binding ElementName=textBox2, Path=(Validation.HasError)}" Value="false" />
<Condition Binding="{Binding ElementName=TextBoxAge, Path=(Validation.HasError)}" Value="false" />
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="true" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
.cs class
public class TextBoxNotEmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string str = value as string;
if (str != null)
{
if (str.Length > 0)
return ValidationResult.ValidResult;
}
return new ValidationResult(false, Message);
}
public string Message { get; set; }
}
If I understand you right you're looking for something like this:
Add this namespace
Update
Then this will work:
And this Converter code