Lorenzo Ang
Lorenzo Ang

Reputation: 1318

In C# WPF, how do I set a label to be highlighted in a certain color AND add a mouse event handler programmatiacally?

So I've set up a TreeView and inside it, I've placed Labels which I need to be highlighted permanently in either red or green on initialization. (Like in the picture) Does anyone know how to do this programmatically? I instantiate the labels like this

Label l = new Label() { Content = roomnumber };

ALSO!! I've been trying to link it to handle a mousedoubleclick event but doing this doesn't work either. any ideas?

Label l = new Label() { Content = roomnumber, MouseDoubleClick="Window_MouseDoubleClick" };

enter image description here

Upvotes: 0

Views: 997

Answers (1)

Amol Bavannavar
Amol Bavannavar

Reputation: 2072

You can set BackgroundProperty of Label as like below.

//Green Colored Background
Label label = new Label() { Content = roomnumber, Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Green) };
//Red Colored Background
Label label = new Label() { Content = roomnumber, Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red) };

Label also has a MouseDoubleClick event you can subscribe this.

label.MouseDoubleClick += label_MouseDoubleClick;
....
void label_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    ////MessageBox.Show(((Label)sender).Content.ToString());
}

Upvotes: 1

Related Questions