将“ToString”更改为对于密封类
我正在使用一个类:
public sealed class WorkItemType
它的 ToString
很弱(仅显示 Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType
)。
有什么方法可以覆盖它以显示 WorkItemType
的名称?
通常我只会聚合一个新类中的值,但我将其用于 WPF 中的绑定(我希望在组合框中有一个 WorkItemTypes 列表,并将选定的值分配给绑定的 WorkItemType)变量。)
我想我在这里运气不好,但我想我会问。
I have a class I am working with:
public sealed class WorkItemType
It's ToString
is weak (Just shows Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType
).
Is there any way to override this to show the name of the WorkItemType
?
Normally I would just aggregate the value in a new class, but I am using this for bindings in WPF (I want to have a list of WorkItemTypes in a combo box and assign the selected value to a bound WorkItemType
variable.)
I think I am out of luck here, but I thought I would ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一种相当巧妙的方法可能是向
WorkItemType
对象添加扩展方法。像这样的东西:然后你可以调用类似的东西
A fairly neat way to do it might be to add an extenesion method to the
WorkItemType
object. Something like this:Then you could call something like
您需要覆盖
ToString
吗?如果您可以控制显示对象的代码,则始终可以提供FormatWorkItemType
方法或达到此效果的方法。Do you need to override
ToString
? If you are in control of the code where the object is displayed, you can always provide aFormatWorkItemType
method, or something to that effect.WPF 提供了几种不同的内置方法来在 UI 中正确执行此操作。我推荐两个:
DisplayMemberPath 显示单个
属性值但仍从中选择
WorkItemType 对象。
您可以更改一些属性
ComboBox 的 ItemTemplate 来制作
看起来很漂亮,随心所欲 -
设置文本格式、添加边框、
颜色等。您甚至可以设置
DataTemplate 自动成为
应用于任何 WorkItemType 对象
绑定在你的 UI 中的任何地方
(与 UI 的基本效果相同
改变 ToString 的视角)
将其投入资源并给予
它只是一个没有 x:Key 的数据类型。
WPF provides a few different built-in ways to do this right in the UI. Two I'd recommend:
DisplayMemberPath to display a single
property value but still select from
the WorkItemType objects.
a few properties you can change the
ComboBox's ItemTemplate to make it
look pretty much however you want -
formatting text, adding borders,
colors, etc. You can even set up the
DataTemplate to automatically be
applied to any WorkItemType object
that gets bound anywhere in your UI
(same basic effect from UI
perspective as changing ToString) by
putting it into Resources and giving
it only a DataType with no x:Key.
你运气不好:-(
你可以编写自己的类来包装 WorkItemType 并委托给它(代理),除了 ToString:
You're out of luck :-(
You could write your own class that wraps the WorkItemType and delegate down to it (a proxy) expect for the ToString:
我没有任何 C# 知识,但是你不能将你的扩展类包装在另一个类中吗?代理对扩展类的所有方法调用,除了 toString() 之外,也非常hackish,但我想无论如何我都会提出它。
I don't have any C# knowledge, but can't you wrap your extended class inside another class? Proxy all method calls to the extended class, except
toString()
, Also very hackish, but I thought I'ld bring it up anyway.通过反思做一些魔术可能是你唯一的希望。我知道你可以用它实例化私有构造函数,所以也许你可以覆盖密封类...注意,如果确实没有其他方法,这应该是你的最后手段。使用反射是一种非常黑客/不正确的方法。
Doing some sorta magic with reflection is probably your only hope. I know you can instantiate private constructors with it, so maybe you can override a sealed class... Note, this should be your last resort if there is seriously no other way. Using reflection is a very hackish/improper way of doing it.
除了其他特定于WPF的答案之外,您还可以在格式绑定中使用
IValueConverter
/ 根据需要显示WorkItemType
。这样做的优点是可重用(例如,如果您想在其他控件中显示对象。)有 这里有许多使用转换器的示例。这个其他问题 应该与此处提到的 ComboBox 用法非常相似。答案指出,您可以使转换器处理整个对象集合,也可以一次处理一个项目。后者可能是更可重用的方法。
In addition to the other WPF-specific answer you could use an
IValueConverter
in the binding to format / display theWorkItemType
however you want. This has an advantage of being reusable (if you want to display the object in some other control, for instance.)There are many examples of using converters here. This other question should be pretty similar to the ComboBox usage mentioned here. The answers note that you can either make the converter work on the entire collection of objects, or work on one item at a time. The latter might be the more reusable approach.