Note that there are some explanatory texts on larger screens.

plurals
  1. POValidate textbox in c# with wpf
    primarykey
    data
    text
    <p>I have a window with some textboxes, comboxes and checkboxes. One of the textboxes need to be a number so I wanted to validate it. I searched through the internet and found a good <a href="http://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation" rel="nofollow">tutorial</a>. I tried to use it, but it seems that it doesn't work or I did something wrong and I just don't see what I did wrong. So I hope anyone here can say me what I'm doing wrong or an other solution to get it started. Here is the xaml of the window:</p> <pre><code>&lt;Window x:Class="WpfApplication1.mainpanels.EditWorkAssignments" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:validators="clr-namespace:WpfApplication1.validationRules" Title="EditWorkAssignments" Height="225" Width="300"&gt; &lt;Window.Resources&gt; &lt;Style TargetType="{x:Type TextBox}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Validation.HasError" Value="true"&gt; &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;ColumnDefinition Width="200" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Label Grid.Row="0" Grid.Column="0" Content="Datum:"/&gt; &lt;Label Grid.Row="1" Grid.Column="0" Content="Projekt:"/&gt; &lt;Label Grid.Row="2" Grid.Column="0" Content="Ist Passiv:"/&gt; &lt;Label Grid.Row="3" Grid.Column="0" Content="Dauer:"/&gt; &lt;Label Grid.Row="4" Grid.Column="0" Content="Mitarbeiter:"/&gt; &lt;DatePicker Name="datePicker" Grid.Column="1" Grid.Row="0" Margin="3" /&gt; &lt;ComboBox Name="comboBoxProject" Grid.Column="1" Grid.Row="1" Margin="3" /&gt; &lt;CheckBox Name="checkBoxIsPassiv" Grid.Column="1" Grid.Row="2" Margin="3" /&gt; &lt;TextBox Name="textBoxDauer" Grid.Column="1" Grid.Row="3" Margin="3" &gt; &lt;Binding Path="workAssignment.duration" UpdateSourceTrigger="PropertyChanged"&gt; &lt;Binding.ValidationRules&gt; &lt;validators:IsNumberValidationRule ErrorMessage="Dauer has to be a number." /&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/TextBox&gt; &lt;ComboBox Name="comboBoxEmployee" Grid.Column="1" Grid.Row="4" Margin="3"&gt; &lt;ComboBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding StringFormat="{}{0} {1}"&gt; &lt;Binding Path="firstname"/&gt; &lt;Binding Path="surname"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/ComboBox.ItemTemplate&gt; &lt;/ComboBox&gt; &lt;Button Grid.Column="1" Grid.Row="5" HorizontalAlignment="Left" MinWidth="80" Margin="3" Content="Save" Click="saveHandler"/&gt; &lt;Button Grid.Column="1" Grid.Row="5" HorizontalAlignment="Right" MinWidth="80" Margin="3" Content="Cancel" Click="cancelHandler" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>the code:</p> <pre><code>using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1.mainpanels { /// &lt;summary&gt; /// Interaction logic for EditWorkAssignments.xaml /// &lt;/summary&gt; public partial class EditWorkAssignments : Window { EmployeeManagementEntities1 context = null; public WorkAssignement workAssignment; public EditWorkAssignments(WorkAssignement workAssignment) { InitializeComponent(); this.workAssignment = workAssignment; context = new EmployeeManagementEntities1(); DbSet&lt;Employee&gt; employeeDb = context.Set&lt;Employee&gt;(); employeeDb.Load(); comboBoxEmployee.ItemsSource = employeeDb.Local; DbSet&lt;Project&gt; projectDb = context.Set&lt;Project&gt;(); projectDb.Load(); comboBoxProject.ItemsSource = projectDb.Local; comboBoxProject.DisplayMemberPath = "projectname"; } private void saveHandler(object sender, RoutedEventArgs e) { Employee employee = (Employee)comboBoxEmployee.SelectedItem; Project project = (Project)comboBoxProject.SelectedItem; context.SaveChanges(); Console.WriteLine("saveHandler"); } private void cancelHandler(object sender, RoutedEventArgs e) { this.Close(); Console.WriteLine("cancelHandler"); } } } </code></pre> <p>and the validationRule:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Globalization; using System.Text.RegularExpressions; namespace WpfApplication1.validationRules { public class IsNumberValidationRule : ValidationRule { private string _errorMessage; public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; } } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { ValidationResult result = new ValidationResult(true, null); string inputString = (value ?? string.Empty).ToString(); try { double.Parse(inputString); } catch(FormatException ex) { result = new ValidationResult(false, this.ErrorMessage); } return result; } } } </code></pre>
    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