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.