question

SaiTeja-0091 avatar image
0 Votes"
SaiTeja-0091 asked HuiLiu-MSFT edited

can i set single property using sender as object and also bind property from Xaml

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed 13 hours ago.

(Private feedback for you)

Unable to apply value for bind property if Object is used.

Void buttonClick(object sender){
chnageColor = Gray; // turns all label foreground to gray
Label l1 = sender as Label;
l1.foreground = blue;
MessageBox.show("Label 1 clicked");
chnageColor = Gray; // turns all label foreground to blue
}

when i try to set color to l1 label using chnageColor (foreground={Binding changecolor}) it is not working.

XAML:-

     </Label>
     <Label x:Name="Label2" Width="200" Height="40" Foreground="{Binding chnageColor}">
         <Label.InputBindings>
             <MouseBinding Command="{Binding buttonClick}" CommandParameter="{x:Reference Label2}" MouseAction="LeftClick" />
         </Label.InputBindings>
     </Label>/

     </Label>
     <Label x:Name="Label3" Width="200" Height="40" Foreground="{Binding chnageColor}">
         <Label.InputBindings>
             <MouseBinding Command="{Binding buttonClick}" CommandParameter="{x:Reference Label3}" MouseAction="LeftClick" />
         </Label.InputBindings>
     </Label>/
windows-wpf
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.

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 foreground of Label you could try to refer to the code below.
The MouseLeftButtonDown event of the Label changes the foreground of the clicked Label. If I misunderstood what you mean, please point it out and let me know .
The code of xaml is as follows:

 <StackPanel>
         <Label Name="Label1" Height="30" Content="Label1" MouseLeftButtonDown="Button_Click" Foreground="{Binding changeColor}"> </Label>
         <Label Name="Label2" Height="30" Content=" Label2" MouseLeftButtonDown="Button_Click" Foreground="{Binding changeColor}" > </Label>
     </StackPanel>

The code of xaml.cs is as follows:

 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;
 using System.Windows.Media;
 namespace ChangeLabelForegroundColorBindCommand
 {
   public partial class MainWindow : Window
   {
     public string changeColor { get; set; }
     public MainWindow()
     {
       InitializeComponent();
       changeColor = "gray";
       DataContext = this;
     }
     private void Button_Click(object sender, MouseButtonEventArgs e)
     {
          Label label = sender as Label;
          label.Foreground = Brushes.Blue;
          MessageBox.Show(label + " is clicked");
          label.Foreground = Brushes.Gray;
     }
   }
 }

The result is shown in the figure:
117407-7.gif


7.gif (117.2 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.