【问题标题】:How to pull out the item and subitems of the listview selected entry如何拉出listview选中条目的item和subitem
【发布时间】:2019-04-10 10:33:40
【问题描述】:

我想编辑一个由 2 个整数和 3 个字符串组成的列表视图项。我正在将项目从公共课程“Rineitem”保存到列表视图中。我可以将选定的行分配给一个对象并在我的本地窗口中查看它,但我不知道如何访问它或它的子项。

我试图找到示例,但没有找到应该如此简单的东西。我自己的尝试经常给出信息,也许我忘记了演员表!???如果我将对象转换为字符串,我会得到一个命名公共类的文本。

object item = lvw_Smd_Jobs.SelectedItem;

当我尝试将 lvw selectedItem 分配给我得到的类时,无法将类型“object”隐式转换为“Rin...Auftrag_Verwalter.Rineitem”。存在显式转换(您是否缺少演员表?) 我想将两个字符串值保存到文本框中,用户可以在其中更改值,然后我会保存列表视图项及其更改。

【问题讨论】:

  • 您可以将 selecteditem 绑定到 rineitem 类型的公共属性。如果您想在选择更改时执行某些操作,请在设置器中执行操作。 social.technet.microsoft.com/wiki/contents/articles/…
  • 你试过 Rin...Auftrag_Verwalter.Rineitem item = lvw_Smd_Jobs.SelectedItem; ?

标签: c# wpf listview


【解决方案1】:

一,为什么不将所有项目都保存为字符串,这样可以简化您的投射。

然后删除一个选定的项目。您将执行以下操作

var item = (string)lvw_Smd_Jobs.SelectedItem; lvw_Smd_Jobs.Items.Remove(item);

【讨论】:

    【解决方案2】:

    您可以将“ListBoxItem”(类型对象)转换为实际的类。

    这里有一个小例子,如何在ListBox 中添加、读取和修改项目:

    // Example class
    public class RineItem
    {
        public string Name { get; set; }
        public int Id { get; set; }
    
        // Override ToString() for correct displaying in listbox
        public override string ToString()
        {
            return "Name: " + this.Name;
        }
    }
    
    public MainWindow()
    {
        InitializeComponent();
    
        // Adding some examples to our empty box
        this.ListBox1.Items.Add(new RineItem() { Name = "a", Id = 1 });
        this.ListBox1.Items.Add(new RineItem() { Name = "b", Id = 2 });
        this.ListBox1.Items.Add(new RineItem() { Name = "c", Id = 3 });
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Loop through SelectedItems
        foreach (var item in this.ListBox1.SelectedItems)
        {
            // Have a look at it's type. It is our class!
            Console.WriteLine("Type: " + item.GetType()); 
            // We cast to the desired type
            RineItem ri = item as RineItem;
            // And we got our instance in our type and are able to work with it.
            Console.WriteLine("RineItem: " + ri.Name + ", " + ri.Id); 
    
            // Let's modify it a little
            ri.Name += ri.Name;
            // Don't forget to Refresh the items, to see the new values on screen
            this.ListBox1.Items.Refresh();
        }
    }
    

    您面临的错误消息告诉您,没有隐式转换可以将 object 转换为 RineItem。

    有可能的隐式转换(从 int 到 long)。你可以创造你自己的。举个例子:

    public class RineItem2
    {
        public string Name2 { get; set; }
        public int Id2 { get; set; }
    
        public static implicit operator RineItem(RineItem2 o)
        {
            return new RineItem() { Id = o.Id2, Name = o.Name2 };
        }
    }
    

    现在你可以这样做了:

    RineItem2 r2 = new RineItem2();
    RineItem r = r2;
    

    但只有在 RineItem2 类中的每个对象都可以转换为 RineItem 时才应使用此方法。

    objectRineItem 的演员每次都必须工作!因此,您不知道您的对象是什么类型,您不允许这样做:

    object o = "bla bla";
    RineItem r = (RineItem)o; // Not allowed! Will not work!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2019-10-20
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多