C# Tag 属性的常见用途

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1058635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:12:27  来源:igfitidea点击:

Common uses for the Tag property

c#wpfwinformssilverlight

提问by Matthew Scharley

I've started using this alot to link elements of my UI to their data backing class (whatever that might be). What are some of the common uses you put the Tag property to use for?

我已经开始大量使用它来将我的 UI 元素链接到它们的数据支持类(无论是什么)。您将 Tag 属性用于哪些常见用途?

Indeed, do you use it at all? I know I didn't for a very long time.

确实,你真的会用吗?我知道我很久没有这样做了。

采纳答案by Jeff Yates

Just as you describe, the most frequent use of the Tagproperty I have come across and use in both WinForms, WPF and Silverlight is to indicate the real data that the control relates to. This is especially useful on ListViewIteminstances or auto-generated user interface where you want to use the same event handler for multiple objects where only the target data is different (i.e. the action to be performed remains the same).

正如您所描述的,Tag我在 WinForms、WPF 和 Silverlight 中遇到并使用的属性的最常见用途是指示与控件相关的真实数据。这在ListViewItem实例或自动生成的用户界面上特别有用,您希望对多个对象使用相同的事件处理程序,其中只有目标数据不同(即要执行的操作保持不变)。

However, I have also used the Tagto store an enumeration value (though you should avoid value types as it would cause boxing when assigning the value to the Tagproperty) or a string that is then used to determine the action that needs to be performed instead of the data on which to perform it, and in one particular usage, I stored a delegate so that I could auto-generate some buttons and embed their handlers in the Tag(the handler information was supplied in a data driven manner).

但是,我还使用了Tag来存储枚举值(尽管您应该避免使用值类型,因为在将值分配给Tag属性时会导致装箱)或用于确定需要执行的操作的字符串,而不是执行它的数据,在一个特定的用法中,我存储了一个委托,以便我可以自动生成一些按钮并将它们的处理程序嵌入其中Tag(处理程序信息以数据驱动的方式提供)。

I am sure there are many other ways to use Tagand many other ways to replace the uses of Tagwith something more strongly typed, but that's how I've used it.

我确信还有许多其他的使用方法Tag和许多其他方法可以用Tag更强类型的东西替换 的使用,但这就是我使用它的方式。

回答by NotMe

The Tag property is an ancient (in programming language terms) hold over for controls. To my knowledge, it's been used in everything from visual basic, delphi, and pretty much any other gui based language.

Tag 属性是一个古老的(在编程语言术语中)控件。据我所知,它已被用于视觉基础、delphi 和几乎任何其他基于 gui 的语言的所有领域。

It is simply an extra property that allows you to add a numeric value for any reason you want to the control.

它只是一个额外的属性,允许您出于任何原因向控件添加数值。

I've seen it used for everything from a counter to holding a record id that the control is tied to.

我已经看到它用于从计数器到保存控件绑定到的记录 ID 的所有内容。

回答by Henk Holterman

It is a bit of a kludge. It is often used in for instance a TreeView to link a Node to a data element.

这有点混乱。它通常用于例如 TreeView 将节点链接到数据元素。

But I would not over-use it, since it is very public and not very flexible. Note that you can almost always use a Dictionary< Control, ValueType> instead, and have a lot more control that way.

但我不会过度使用它,因为它非常公开而且不是很灵活。请注意,您几乎总是可以使用 Dictionary<Control, ValueType> 来代替,并通过这种方式获得更多控制权。

回答by NascarEd

I use it all the time with ListViews and TreeViews. It makes trying to find the underlying data mucheasier. In fact, I'm not sure how you'd make a readable Winforms application without it.

我一直将它与 ListViews 和 TreeViews 一起使用。它使查找底层数据变得更加容易。事实上,我不确定如果没有它,您将如何制作可读的 Winforms 应用程序。

I also use it a lot when creating ContextMenus at run-time. I stuff an object into the Tag member of each ToolStripMenuItem and then I can point each menu item's click handler at the same method. It results in a lot less code.

在运行时创建 ContextMenus 时,我也经常使用它。我将一个对象填充到每个 ToolStripMenuItem 的 Tag 成员中,然后我可以将每个菜单项的单击处理程序指向相同的方法。它导致代码减少了很多。

I just wish it didn't require so much casting.

我只是希望它不需要那么多铸造。