The second approach is the way to go. In your viewmodel, add a ICommand DoOnTextChanged
and dependency property BackgroundColor
.
- Bind the
DoOnTextChanged
command with the TextChanged event of TextBox1 using Behaviours
- Bind the
BackgroundColor
property to the background of TextBox2 using converter.
- In the Execute function of
DoOnTextChanged
, change the BackgroundColor
property and you are done.
If you are using MVVMLight, binding to ICommand is easy. First add these two namespace xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
and xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
and do the following:
<TextBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged" >
<cmd:EventToCommand Command="{Binding DoOnTextChanged}" PassEventArgsToCommand="False" >
</cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Update
As OP is using plain wpf/Xaml, I am updating my answer with implementation for plain wpf.
Add the following two helper class in your project:
public class ExecuteCommand : TriggerAction<DependencyObject>
{
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExecuteCommand));
public ICommand Command
{
get
{
return GetValue(CommandProperty) as ICommand;
}
set
{
SetValue(CommandProperty, value);
}
}
protected override void Invoke(object parameter)
{
if (Command != null)
{
if (Command.CanExecute(parameter))
{
Command.Execute(parameter);
}
}
}
}
public class EventCommand : ICommand
{
private Action<object> func;
public EventCommand(Action<object> func)
{
this.func = func;
}
public bool CanExecute(object parameter)
{
//Use your logic here when required
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (func != null)
{
func(parameter);
}
}
}
In your ViewModel, implement INotifyPropertyChanged and add the following ICommand and Background property.
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public MainViewModel(IDataService dataService)
{
BackColor = Brushes.Aqua;
DoOnTextChanged = new EventCommand((obj => BackColor = BackColor == Brushes.BurlyWood ? Brushes.Chartreuse : Brushes.BurlyWood));
}
public ICommand DoOnTextChanged { get; set; }
private Brush backColor;
public Brush BackColor
{
get
{
return backColor;
}
set
{
backColor = value;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BackColor"));
}
}
}
}
Finally, in you ViewName.xaml file, add this namespace xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
. You may need to add a reference to System.Windows.Interactivity. Then add the following to bind the button event to a command:
<TextBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged" >
<local:ExecuteCommand Command="{Binding DoOnTextChanged}"></local:ExecuteCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<TextBox Background="{Binding BackColor}"></TextBox>
Although it is a lot of code to accomplish something simple, it can be really helpful is certain cases. It is better to learn all the ways and use the one that fits perfectly to your need.