question

SaiTeja-0091 avatar image
0 Votes"
SaiTeja-0091 asked GaetanoSblendorio-7317 commented

WPF Sender as Object and Comand parameter conflict

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?

dotnet-csharpwindows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you would like to change Label properties like the Foreground color you can also use Triggers. They allows you to control the appearance of your control and react to mouse "events" without write any lines of code in the code behind.


0 Votes 0 ·

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT edited

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



13.gif (89.0 KiB)
14.gif (238.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.