0

In UI you can do this;

private void Button_Click(object sender, RoutedEventArgs e){
   MyTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}

But with binding Command, how do you do that? For example, I have the following;

<TextBox x:Name="MyTextBox">
   <TextBox.Text>
      <Binding
         Mode="TwoWay"
         Path="MyTextBoxDataBinding"
         UpdateSourceTrigger="PropertyChanged">
         <Binding.ValidationRules>
            <validations:MyTextBoxValidation ValidationStep="UpdatedValue" />
         </Binding.ValidationRules>
       </Binding>
    </TextBox.Text>
</TextBox>

<Button Command="{Binding Path=UpdateSourceNow}"/>

and assuming in my ViewModel I have this;

private string? _MyTextBoxDataBinding = string.Empty;
public string? MyTextBoxDataBinding{
    get => _MyTextBoxDataBinding;
    set { SetProperty(ref _MyTextBoxDataBinding, value); }
}
...

private readonly MyButtonCommand _UpdateSourceNow;
public ICommand UpdateSourceNow => _UpdateSourceNow;
private void UpdateSourceNowFunction(object commandParamater){
   //update the source of MyTextBoxDataBinding and notify UI
}

public MyViewModel(){
   _UpdateSourceNow= new MyButtonCommand(UpdateSourceNowFunction);
}

I have a ValidationRule in my TextBox which validates an empty value, it also shows an error message below the TextBox but I don't want it to be shown at the first load of the Window. Now, on the Button Click, the ValidationRule should be triggered. But it seems the only way to trigger it if the TextBox is never been touched is by updating the source. But how can I do this in my ViewModel so I don't have to do it directly on the UI?

Also, I have a DataGrid and the generated columns contain TextBoxes. I'd like to set the same ValidationRule so it should worth doing this in ViewModel instead of the UI.

Polar
  • 3,327
  • 4
  • 42
  • 77
  • You should consider instead of getting the information from the UI, to get the information from the data behind. Imagine you have a datagrid with 10 million of rows, it would need to load every UI element to render the every single row. Which isn’t really a light operation. – Rand Random Jul 28 '22 at 01:50
  • Actually, I am trying to do is; On `Button_Click`, make sure all the `TextBox` added on the `DataGrid` has a value. I also have a `ValidationRule` on the `TextBox` which generates a text error in those TextBoxes, the `ValidationRule` should be triggered with the above method. – Polar Jul 28 '22 at 01:59
  • I also have an `ICommand` bind into the `Button`, but I couldn't find a way to trigger my `ValidationRule` from my `ViewModel`. Anyway, my `DataGrid` is allowed only to have 50 items. – Polar Jul 28 '22 at 02:00
  • To validate your view model, you should always prefer to implement INotifyDataErrorInfo over binding validation. This enables property validation e.g. on property changes directly in the view model: [How to add validation to view model properties or how to implement INotifyDataErrorInfo](https://stackoverflow.com/a/56608064/3141792) – BionicCode Jul 30 '22 at 06:36

0 Answers0