I have a case where say I have one property that has a string which contains a separator (e.g. ':'). I have 3 text boxes and I would like to share each separated part value in each textbox and also update them when one of the textboxes value changes. How can I implement this behavior in WPF using data binding?
My case involves using Prism and MVVM, my approach is as follows:
- Model class which inherits from bindable base
- View model will also inherits from bindable base
- Model instance will be created in the view model's constructor and be used as
DataContextwhere it is needed
See this sample code here:
In my model class:
private string _someValue = "A:B:C";
public string SomeValue
{
get => _someValue;
set => SetProperty(ref _someValue, value);
}
SomeValue. MakeSomeValuereadonly and return the combination likepublic string SomeValue => $"{SomeValuePartA}:{SomeValuePartB}:{SomeValuePartC}";SomeValue => $"{SomeValuePartA}:{SomeValuePartB}:{SomeValuePartC}";this will be in the set property of the SomeValue? like this ` set => SetProperty(ref $"{SomeValuePartA}:{SomeValuePartB}:{SomeValuePartC}", value);`SomeValuecould have a setter, though, if needed, that does the splitting and updates the other three properties.