WPF Sender as Object and Comand parameter conflict

Sai Teja 1 Reputation point
2021-07-21T11:57:51.16+00:00

1) I am having multiple labels
2) assigned mouse click event
3) each label have foreground={Binding changeColor}
4) And ViewModel i am collecting specific label object on mouse click using X:Refference

suppose label1 clicked
public void onMouceClick(object sender)
{
changeColor = Brushes.White;

selectedlabel = sender as Label;

selectedlabel.foreground = Brushes.Blue;

Message_Box(" selected label is : label1 ");

//after click Ok from message box

changeColor = Brushes.White;

}
After message box click OK
I am unable to change forground of the label1 using forground Binding property which is changeColor.

is it possible to change properties of a lable using both object and Binding properties?

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2021-07-22T08:39:18.903+00:00

    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.
    117016-14.gif

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.