【问题标题】:How to attach extra data to listbox items in VB.Net如何在 VB.Net 中将额外数据附加到列表框项
【发布时间】:2013-09-30 15:16:44
【问题描述】:

我是 Visual Studio 的新手,正在从事 Visual Basic 项目。我正在将数据库中的数据添加到列表框,我需要稍后访问。我们可以像使用以下 html 一样向列表框项目添加额外的数据吗??

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

有什么想法吗???

问候

【问题讨论】:

  • 如果您正在使用 WPF,请阅读this

标签: wpf vb.net listbox


【解决方案1】:

您不会将任何数据(无论这意味着什么)“附加”到 WPF 中的任何 UI 元素,仅仅是因为 UI is not Data

如果您使用 WPF,您确实需要了解 The WPF Mentality,这与其他技术中使用的其他方法非常不同。

在 WPF 中,您使用 DataBinding 将 UI“绑定”到数据,而不是在 UI 中“放置”或“存储”数据。

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

XAML:

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

视图模型:

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。否则你会很快碰壁并在不需要的代码上浪费太多时间。

【讨论】:

    猜你喜欢
    • 2019-12-30
    • 2016-03-30
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2019-09-08
    相关资源
    最近更新 更多