question

StashyCode-7539 avatar image
0 Votes"
StashyCode-7539 asked StashyCode-7539 edited

Complex TextBox Input Validation WPF MVVM

I have a ListBox in my main window and a button for adding a property to my ListBox:

 <ListBox
     //other stuff
     ItemsSource="{Binding ViewModels, UpdateSourceTrigger=PropertyChanged}"
     SelectedItem="{Binding SelectedThing, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
    
 <Button Click="OnAddClick" Content="Add"/>


MainWindow.xaml.cs:

 private void OnAddClick(object sender, EventArgs e)
 {
       var addDialogBox = new AddView(((MainViewModel)DataContext).AddViewModel);
       addDialogBox.ShowDialog();
 }


The button opens a new window for the adding of the element. This view has a TextBox for entering one of the properties: a string, composed of integers, separated by a comma - for example 1-5, 15, 20 => this will be deserialized into a List<int> in the MainViewModel. The integers are not supposed to exceed a value in the MainViewModel - MaxPossibleValue.

I need to create a validation in the newly opened window, making sure that correct input has been entered.

AddWindow.xaml:

 <DockPanel LastChildFill="True">
         <StackPanel
             Height="40"
             HorizontalAlignment="Right"
             DockPanel.Dock="Bottom"
             Orientation="Horizontal">
             <Button
                 Command="{Binding SaveCommand}"
                 Content="Submit"
                 IsDefault="True" />
         </StackPanel>
             <TextBox
                 Name="PropertyTextBox"
                 Text="{Binding SomeProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
 </DockPanel>


AddWindow.xaml.cs:

 public AddWindow()
 {
      InitializeComponent();
 }
    
 public AddWindow(AddViewModel context) : this()
 {
      DataContext = context;
      context.Save += Save;
 }
    
 private void Save(object sender, EventArgs e)
 {
      Close();
 }

AddViewModel.cs:

 public string SomeProperty { get; set; }
    
 public event EventHandler Save;
    
 public RelayCommand<object> SaveCommand { get; set; }
    
 public AddViewModel()
 {
       SaveCommand = new RelayCommand<object>(x => Save(this, new EventArgs()));
 }

MainViewModel.cs:

 public MainViewModel(AddViewModel addViewModel)
 {
       AddViewModel = addViewModel ?? throw new ArgumentNullException(nameof(addViewModel));
       AddViewModel.Save += ViewModelOnAddSave;
 }
    
 private void ViewModelOnAddSave(object sender, EventArgs eventArgs)
 {
        //this will throw an exception and break if the input is not correct
        var groupFromSomeProperty = AddViewModel.SomeProperty.Split(new string[] { ", " }, StringSplitOptions.None)
                                 .Select(i => i.Split('-').Select(s => int.Parse(s)).ToArray())
                                 .SelectMany(i => i.Length == 1 ? i : Enumerable.Range(i[0], i[1] - i[0] + 1)).ToList();
    
        ViewModels.Add(new SomeNotImportantType
        {
              ListOfIntegers = groupFromSomeProperty 
        });
 }


AddViewModel is injected into MainViewModel using dependency injection and the adding to the ListBox's ItemsSource is performed there.

I want to validate it after clicking on Submit button, but the Window should remain open until a successful action has been made (the user corrects himself or the user closes the entire window on his own).

How can I achieve that?

windows-wpfdotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@StashyCode-7539. Have you deserialized the text of the TextBox? Could you show the code of your MainViewModel? It is helpful to analyze the problem if you could provide a more complete code.

0 Votes 0 ·

Hi, @HuiLiu-MSFT, just added the code in the MainViewModel.

0 Votes 0 ·

0 Answers