如何将额外的数据附加到VB.Net中的列表框项目 [英] How to attach extra data to listbox items in VB.Net

查看:74
本文介绍了如何将额外的数据附加到VB.Net中的列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Visual Studio的新手,正在从事Visual Basic项目.我正在将数据从以后需要访问的数据库添加到列表框中..我们可以像在下面的html

i'm new to visual studio and working on a project in visual basic. I am additing data to listbox from database that i need to access later.. Can we add extra data to listbox items as we can do with following html ??

<option name="harry" age="10" value="1">My name is harry</option>

有什么想法吗?

致谢

推荐答案

您不会将任何数据(无论是什么意思)附加"到WPF中的任何UI元素,仅仅是因为

You don't "attach" any data (whatever that means) to any UI elements in WPF, simply because UI is not Data.

如果您使用的是WPF,则确实需要了解 WPF的心态 ,这与其他技术中使用的其他方法截然不同.

If you're working with WPF, you really need to understand The WPF Mentality, which is very different from other approaches used in other technologies.

在WPF中,您使用数据绑定来绑定"用户界面到数据,而不是在用户界面中放入"或存储"数据.

In WPF, you use DataBinding to "Bind" the UI to the Data, as opposed to "putting" or "storing" the data in the UI.

这是如何将ListBox绑定到WPF中的数据项集合的一个示例:

This is an example of how you Bind a ListBox to a collection of data items in WPF:

XAML:

<ListBox ItemsSource="{Binding MyCollection}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding FirstName}"/>
                <TextBlock Text="{Binding LastName}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

ViewModel:

ViewModel:

public class MyViewModel
{
    public ObservableCollection<MyData> MyCollection {get;set;}

    //methods to create and populate the collection.
}

数据项:

public class MyData
{
    public string LastName {get;set;}

    public string FirstName {get;set;}
}

我强烈建议您在开始使用WPF进行编码之前先阅读MVVM.否则,您会很快撞墙,并在不必要的代码中浪费太多时间.

I strongly suggest that you read up on MVVM before starting to code in WPF. Otherwise you'll hit walls quickly and waste too much time in unneeded code.

这篇关于如何将额外的数据附加到VB.Net中的列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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