How is it possible to use a var after "this."
i want to set the Background color of a TextBlock to Blue.
Brush blue = new SolidColorBrush(Colors.LightBlue); //Working Fine
//Click on the TextBlock (Working Fine)
private void MouseDown_TextBlock(object sender, MouseButtonEventArgs e)
{
var stand = ((TextBlock)sender).Name; //Here i am getting the Name of the TextBlock (Working Fine)
this.stand.Background = blue; //This is not working here
}
>Solution :
If you want to access the control you receive as Sender in an event, just cast it to the appropriate class:
private void textBlock1_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get sender as TextBlock
TextBlock standTextBlockFromSender = (TextBlock)sender;
standTextBlockFromSender.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
}
If you want to find the control by name, you can use the FindName
method.
private void textBlock1_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get name of sender
string stand = ((TextBlock)sender).Name;
// Get TextBlock by name
TextBlock standTextBlockFromName = (TextBlock)this.FindName(stand);
standTextBlockFromName.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
}