I am not sure if I fully understood what needs to happen, if you want to change the color of Label you could try to refer to the code below. And you can also refer to more details about InputBinding.
The code of xaml is as follows:
<StackPanel>
<Label Name="Label1" Height="30" Content="Blue" MouseLeftButtonDown="Label1_Click">
<Label.InputBindings>
<MouseBinding Command="{Binding ChangeColorCommand}" CommandParameter="{Binding ElementName= Label1,Path=Content}" MouseAction="{Binding ChangeColorCommand.MouseGesture}" />
</Label.InputBindings>
</Label>
<Label Name="Label2" Height="30" Content="Red" >
<Label.InputBindings>
<MouseBinding Command="{Binding ChangeColorCommand}" CommandParameter="{Binding ElementName= Label2,Path=Content}" MouseAction="{Binding ChangeColorCommand.MouseGesture}" />
</Label.InputBindings>
</Label>
</StackPanel>
The code of xaml.cs is as follows:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace ChangeLabelForegroundColorBindCommand
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeCommand();
}
public SimpleDelegateCommand ChangeColorCommand
{
get { return changeColorCommand; }
}
private SimpleDelegateCommand changeColorCommand;
private void InitializeCommand()
{
originalColor =Brushes.Black;
changeColorCommand = new SimpleDelegateCommand(x => this.ChangeColor(x));
Label1.DataContext = this;
Label2.DataContext = this;
ChangeColorCommand.MouseGesture = MouseAction.RightClick;// Click the right button to switch colors
//ChangeColorCommand.MouseGesture = MouseAction.LeftClick;// Click the left button to switch colors
}
private Brush originalColor, alternateColor;
private void Label1_Click(object sender, MouseButtonEventArgs e)
{
var label = sender as Label;
label.Foreground = Brushes.Green;
MessageBox.Show(sender.ToString());
if (MessageBox.Show(sender.ToString(), "click ok to yellow,click cancel to orane", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
label.Foreground = Brushes.Yellow;
}
else
{
label.Foreground = Brushes.Orange;
}
}
public void ChangeColor(object colorString)
{
if (colorString == null)
{
return;
}
Color newColor = (Color)ColorConverter.ConvertFromString((String)colorString);
alternateColor = new SolidColorBrush(newColor);
if (this.Label1.Foreground == originalColor)
{
this.Label1.Foreground = alternateColor;
}
else
{
this.Label1.Foreground = originalColor;
}
}
}
public class SimpleDelegateCommand : ICommand
{
public MouseAction MouseGesture { get; set; }
Action<object> _executeDelegate;
public SimpleDelegateCommand(Action<object> executeDelegate)
{
_executeDelegate = executeDelegate;
}
public void Execute(object parameter)
{
_executeDelegate(parameter);
}
public bool CanExecute(object parameter) { return true; }
public event EventHandler CanExecuteChanged;
}
}
The result is shown in the figure:
You can click the right button to change the color of Label1, and click the left button to display the MessageBox.
The color of Label1 changes to blue when Label1 is clicked. Click Label1 again to restore the color.
The color of Label1 changes to red when Label2 is clicked. Click Label2 again to restore the color.