我可以访问,而不是ListViewItem.Tag的性质只是"&的ToString QUOT;方法? [英] Can I access properties of a ListViewItem.Tag rather than just the "ToString" method?

查看:276
本文介绍了我可以访问,而不是ListViewItem.Tag的性质只是"&的ToString QUOT;方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要有耐心,不能确定如何最好地问这一个...

Be patient, not sure how best to ask this one ...


  1. 我有一个Windows窗体上的ListView控件,我将项目添加到它属于文本文件的某个目录的名字。

  2. 每个这些文件被加载作为对象ResultFile,被分配基于文本内许多属性 - 字符串类型,INT NumberOfLines,布尔IsGeneric等

  3. 用户可以选择/取消选择自由,但我想提出基础上,他们已经选择了文本文件类型决定。作为一个粗略的想法,如果他们已经选择了一个,其中类型==X1我不希望他们能够选择另外一个。如果他们选择2个或以上,其中IsGeneric ==真的,我想给他们警告...

我开始通过延伸与此ListViewItem的

I started by extending the ListViewItem with this

public class MyListViewItem : ListViewItem
{
    public string Type { get; set; }
    public int NumberOfLines { get; set; }
    public bool IsGeneric { get; set; }

    public MyListViewItem(string s)
        : base(s)
    { }
}

我很满意这个因为这是我第一次延长控制(是的,我是新来这个)......这让我补充的ListViewItems我自己的类型,我可以访问性能做决定,但我找到了标签财产和想我可能只是配合我对象并获取他们的项目直接。我看着 MSDN 和它说我可以将ANY的对象,但我不知道如何使用它。

I was happy with this as it was the first time I've extended a control (yes, I'm new to this) ... This allows me to add my own types of "ListViewItems" and I can access the properties to make decisions but I found the "tag" property and thought I could just tie my object to it and access they items directly. I looked at MSDN and it said I can attach "ANY" object but I don't know how to use it.

我似乎不能够做任何事情,除了访问的默认对象的方法。

I don't seem to be able to do anything except access the default object methods.

     myListView.Item[1].Tag.ToString();

似乎是我所能做的最...

Seems to be the most I can do ...

我缺少一些关于ListViewItem.Tag物业??

Am I missing something about the ListViewItem.Tag Property??

推荐答案

标签属性采用键入 对象,所以任何类是一个对象可以被存储在其中。所以,如果你有这样一个对象:

The Tag property takes a Type of object, so any class that is an object can be stored within it. So if you had an object like this:

public class MyItem
{
    public string Type { get; set; }
    public int NumberOfLines { get; set; }
    public bool IsGeneric { get; set; }
}

您可以标记等于设置为你的对象,像这样:

You can set the tag equal to your object, like so:

var myobj = new MyItem();
myobj.Type = "Type 1";
myListView.Item[1].Tag = myobj;

和检索对象,像这样:

var myobj = (MyItem)myListView.Item[1].Tag;
var type = myobj.Type;

在投射到 MyItem 只有在你能访问您的自定义属性。否则,你就只能获得对象属性和方法,其中一个是的ToString()

Only after you cast to MyItem can you access your custom properties. Otherwise you'll only get the object properties and methods, one of which is .ToString().

这篇关于我可以访问,而不是ListViewItem.Tag的性质只是"&的ToString QUOT;方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆