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

Sai Teja 1 Reputation point
2021-07-22T08:01:53.737+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,772 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,531 Reputation points Microsoft Vendor
    2021-07-23T09:45:58.343+00:00

    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

    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.