简体   繁体   English

在C#WPF中,如何设置以某种颜色突出显示的标签并以编程方式添加鼠标事件处理程序?

[英]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. 因此,我建立了一个TreeView并在其中放置了Labels,在初始化时需要将其永久性地用红色或绿色突出显示。 (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. 我一直试图将其链接以处理mousedoubleclick事件,但是这样做也不起作用。 any ideas? 有任何想法吗?

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

在此处输入图片说明

You can set BackgroundProperty of Label as like below. 您可以如下设置Label BackgroundProperty

//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. 标签也有一个MouseDoubleClick事件,您可以订阅此事件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VS2015 C# WPF:如何添加事件处理程序? - VS2015 C# WPF: How to add event handler? C#WPF:添加鼠标输入和鼠标离开事件处理程序后,ListBox项目未突出显示 - C# WPF: ListBox item not getting highlighted after adding mouse enter and mouse leave event handlers 如何将对象传递给 WPF 中的按钮单击事件处理程序? C# - How do I pass an object to a button click event handler in WPF? C# 在 wpf c# 中按下 Enter 键时,如何设置标签并将整数值从 TextBox 显示到标签? - How do i set the label and display an integer value from a TextBox to the label when the enter key is pressed in wpf c#? 如何创建在用户设置的特定日期和时间弹出的WPF C#应用程序? - How do I create a WPF C# Application that pops up at a certain date and time set by the user? 将外部事件处理程序添加到C#/ Xaml中的WPF组件? - Add external event handler to WPF component in C#/ Xaml? C#WPF为另一个页面中的处理程序的窗口添加KeyUp事件 - C# WPF add KeyUp event for window with a handler in another page WPF 我怎样才能用 1 个事件处理程序做到这一点 - WPF How can i do it with 1 event handler 如何在c#中添加事件处理程序? - How to add event handler in c#? 如何在 WPF、C# 中自动生成事件处理程序? - How to generate event handler automatically in WPF, C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM