【问题标题】:Databinding 101. How to do it in .Net数据绑定 101. 如何在 .Net 中进行
【发布时间】:2011-11-12 23:34:15
【问题描述】:

这似乎是一件显而易见的事情,但我就是想不通。假设我有一个字符串列表。如何将它绑定到列表框,以便列表框随着列表数据的变化而更新?我正在使用 vb.net。

到目前为止,我已经尝试过了。我已经设法显示数据,但没有改变它:

Public Class Form1

    Private mycountries As New List(Of String)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        mycountries.Add("Norway")
        mycountries.Add("Sweden")
        mycountries.Add("France")
        mycountries.Add("Italy")
        mycountries.Sort()
        ListBox1.DataSource = mycountries 'this works fine

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        mycountries.RemoveAt(0)
        ListBox1.DataSource = mycountries 'this does not update
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MsgBox(mycountries(0))
    End Sub
End Class

我也试过这个:

ListBox1.DataBindings.Add("items", mycountries, "Item")

但是 items 是只读的,所以它不起作用。

另外,如果我想将按钮的启用属性绑定到布尔值,我该怎么做? 我已经尝试过了,但我不知道要为最后一个参数添加什么。

Dim b As Boolean = True
Button3.DataBindings.Add("Enabled", b, "")

【问题讨论】:

标签: .net vb.net winforms data-binding


【解决方案1】:

你可以做到这一点:

ListBox1.DataSource = Nothing
mycountries.RemoveAt(0)
ListBox1.DataSource = mycountries

【讨论】:

    【解决方案2】:

    您需要使用支持数据源更改通知的集合。当您从普通列表中删除一个项目时,它不会告诉任何人。

    下面是一个使用BindingList 的示例:

    Public Class Form1
    
        Private mycountries As New BindingList(Of String)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            mycountries.Add("Norway")
            mycountries.Add("Sweden")
            mycountries.Add("France")
            mycountries.Add("Italy")
            ' BindingList doesn't have a Sort() method, but you can sort your data ahead of time
            ListBox1.DataSource = mycountries 'this works fine
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            mycountries.RemoveAt(0)
            ' no need to set the DataSource again here
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            MsgBox(mycountries(0))
        End Sub
    End Class
    

    对于第二个问题,添加数据绑定时不要绑定到变量。您绑定到一个对象(充当数据源),然后在该对象上指定一个属性,该属性将为您提供要绑定到的值。

    所以,对于一个按钮,你想要这样的东西(为 C# 道歉,但你会明白的):

    public class SomeModel
    {
        public bool ButtonEnabled { get; set; }
    }
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SomeModel model = new SomeModel();
    
            // first parameter - button's property that should be bound
            // second parameter - object acting as the data source
            // third parameter - property on the data source object to provide value
            button1.DataBindings.Add("Enabled", model, "ButtonEnabled");
    }
    

    一般来说,数据绑定都是关于更改通知的。如果您需要绑定到自定义对象,请查看INotifyPropertyChanged 接口。

    【讨论】:

    • 完美!非常感谢!与以正常方式更改按钮相比,在使用此类方法时,我是否必须注意任何性能注意事项?
    • @Johan 我不知道。即使在相当繁忙的表单上,我也从未见过对性能有任何影响。不要忘记,您需要为要绑定的属性实现INotifyPropertyChanged,以便在更改属性值时更新按钮的状态。
    猜你喜欢
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多