WPF ComboBox绑定无法按预期工作

时间:2008-10-27 18:22:48

标签: wpf vb.net data-binding xaml combobox

我希望我的WPF ComboBox的ItemsSource属性绑定到MyListObject的MyList属性。问题是当我在代码中更新MyList属性时,WPF ComboBox没有反映更新。我在执行更新后提升了PropertyChanged事件,我认为WPF应该通过更新UI自动响应。我错过了什么吗?

这是CLR对象:

Imports System.ComponentModel

Public Class MyListObject
    Implements INotifyPropertyChanged

    Private _mylist As New List(Of String)

    Public Sub New()
        _mylist.Add("Joe")
        _mylist.Add("Steve")
    End Sub

    Public Property MyList() As List(Of String)
        Get
            Return _mylist
        End Get
        Set(ByVal value As List(Of String))
            _mylist = value
        End Set
    End Property

    Public Sub AddName(ByVal name As String)

        _mylist.Add(name)

        NotifyPropertyChanged("MyList")

    End Sub

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, _
            ByVal e As System.ComponentModel.PropertyChangedEventArgs) _
            Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

End Class

这是XAML:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    xmlns:local="clr-namespace:WpfApplication1"
        >

    <Window.Resources>
        <ObjectDataProvider x:Key="MyListObject" ObjectType="{x:Type local:MyListObject}"/>
    </Window.Resources>

        <Grid>

        <ComboBox Height="23"
                  Margin="24,91,53,0"
                  Name="ComboBox1"
                  VerticalAlignment="Top" 
                  ItemsSource="{Binding Path=MyList, Source={StaticResource MyListObject}}"
                  />
        <TextBox Height="23"
                 Margin="24,43,134,0"
                 Name="TextBox1"
                 VerticalAlignment="Top" />
        <Button Height="23"
                HorizontalAlignment="Right"
                Margin="0,43,53,0"
                Name="btn_AddName"
                VerticalAlignment="Top"
                Width="75">Add</Button>
    </Grid>
</Window>

这是简单的代码隐藏:

Class Window1 

    Private obj As New MyListObject

    Private Sub btn_AddName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) _ 
    Handles btn_AddName.Click

        obj.AddName(TextBox1.Text)

    End Sub
End Class

谢谢!

3 个答案:

答案 0 :(得分:3)

您绑定到字符串列表。该列表类不实现Inotifyproperty。您应该使用observablecollection。 我也注意到你的代码背后声明

Private obj As New MyListObject

这不是您将组合框绑定到的静态资源。因此,您的添加调用不会反映在您的视图中。

答案 1 :(得分:2)

ObservableCollection很可能是解决方案,但如果它仍然让您感到悲伤,您可以在列表更新后通过调用以下代码直接访问您的静态资源:

DirectCast(Me.FindResource("MyListObject"), ObjectDataProvider).Source = _myList

答案 2 :(得分:0)

尝试使用BindingList(Of T)代替List(Of T)。

编辑:我是WPF的新手,看起来BindingList看起来并不是解决问题的完整解决方案,但它可能是朝着正确方向迈出的一步。我能够在WinForm中测试转换为BindingList的MyListObject,并将ListChanged事件引发到ComboBox,然后更新其列表。

我发现这个(可能的)解决方案将您的类包装在一个可能帮助您解决问题的ObservableCollection中

Enabling WPF Magic Using WCF - Part 1

这是将对象更新为BindingList的代码。将您的代码与该资源中的代码相结合,您应该很高兴。

Public Class MyListObject
    ...

    'Private _mylist As New List(Of String)
    Private _mylist As New BindingList(Of String)

    ...

    'Public Property MyList() As List(Of String)
    '    Get
    '        Return _mylist
    '    End Get
    '    Set(ByVal value As List(Of String))
    '        _mylist = value
    '    End Set
    'End Property

    Public Property MyList() As BindingList(Of String)
        Get
            Return _mylist
        End Get
        Set(ByVal value As BindingList(Of String))
            _mylist = value
        End Set
    End Property

    ...

End Class