双向WPF绑定到ObservableCollection内的类的Properties

时间:2011-11-17 20:52:03

标签: wpf vb.net xaml binding wpfdatagrid

我尽可能地搜索了,而且我找不到这个具体问题的答案...... WPF绑定看起来很棒,但是我最终还是撞到了墙上通常情况下。

好的,我有一个单例类,它最终是我绑定的那个:

Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class AmandaSeyfried
    Implements INotifyPropertyChanged

    Shared _config As New config

    Public Event PropertyChanged(sender As Object, E As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Protected Overridable Sub OnPropertyChanged(propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Private Shared _thisInstance As AmandaSeyfried

    Protected Sub New()
        ' initialization goes here
    End Sub

    Public Shared Function GetSingleton() As AmandaSeyfried
        ' initialize object if it hasn't already been done
        If _thisInstance Is Nothing Then
            _thisInstance = New AmandaSeyfried
        End If

        ' return the initialized instance
        Return _thisInstance
    End Function

    Public Class CountryTranslation
        Implements INotifyPropertyChanged

        Private Property _englishCountryName As String = ""
        Public Property EnglishCountryName As String
            Get
                Return _EnglishCountryName
            End Get
            Set(value As String)
                If _englishCountryName <> value Then
                    _englishCountryName = value
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("EnglishCountryName"))
                End If
            End Set
        End Property

        Private Property _foreignCountryName As String = ""
        Public Property ForeignCountryName As String
            Get
                Return _foreignCountryName
            End Get
            Set(value As String)
                If _foreignCountryName <> value Then
                    _foreignCountryName = value
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ForeignCountryName"))
                End If
            End Set
        End Property

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

    Private WithEvents _countryTranslations As New ObservableCollection(Of CountryTranslation)
    Public Property CountryTranslations As ObservableCollection(Of CountryTranslation)
        Get
            If _config.GetKeyTextValue("countryTranslations") <> "" Then
                Dim reader As New IO.StringReader(_config.GetKeyTextValue("countryTranslations"))
                Dim Serializer As New Xml.Serialization.XmlSerializer(_countryTranslations.GetType)
                _countryTranslations = Serializer.Deserialize(reader)
            End If

            Return _countryTranslations
        End Get
        Set(value As ObservableCollection(Of CountryTranslation))
            _countryTranslations = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CountryTranslations"))
        End Set
    End Property

    Private Sub CountryTranslationCollectionChanged(sender As Object, e As Specialized.NotifyCollectionChangedEventArgs) Handles _countryTranslations.CollectionChanged
        Dim newStringWriter As New IO.StringWriter
        Dim NewSerializer As New Xml.Serialization.XmlSerializer(_countryTranslations.GetType)
        NewSerializer.Serialize(newStringWriter, _countryTranslations)
        _config.SaveKeyTextValue("countryTranslations", newStringWriter.ToString)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CountryTranslations"))
    End Sub

End Class

_config是一个名不副实的助手类,用于存储和检索本地SqlCe实例中的数据。本质上,对象被序列化,存储在DB中,然后在需要的任何时候从DB中拉出并反序列化为对象。总而言之,它似乎运作得相当好。

我的问题是虽然我可以绑定到该对象,并且我可以通过CollectionChangedMethod处理程序监视何时在WPF DataGrid中添加行,但当CountryTranslation的两个属性中的任何一个发生更改时,我都不会收到任何通知

我的相关代码的其余部分是...... XAML ......显然更多,但我不相信绑定的XAML部分是责备,所以我将它修剪为相关的:

<toolkit:DataGrid Margin="12,12,12,12" ItemsSource="{Binding Path=KarenSmith.CountryTranslations, Mode=TwoWay}" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Width="Auto" SelectionMode="Single">
    <toolkit:DataGrid.Columns>
        <toolkit:DataGridTextColumn Width="283" Binding="{Binding EnglishCountryName,Mode=TwoWay}" />
        <toolkit:DataGridTextColumn Width="283" Binding="{Binding ForeignCountryName,Mode=TwoWay}" />
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

漂亮而简单的代码隐藏:

Public Class Preferences

    Public Property KarenSmith As AmandaSeyfried = AmandaSeyfried.GetSingleton

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DataContext = Me

    End Sub

    Private Sub Close_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Me.Close()
    End Sub
End Class

如果我在CountryTranslation类的Getter和Setter上抛出一些断点,我可以监视它们何时被更改(通过数据网格,因此绑定工作),但尝试我可能无法弄清楚如何根据主类中的事件引发事件,以便随后更新数据存储区以显示更改。

1 个答案:

答案 0 :(得分:2)

通常我会向CollectionChanged添加一个ObservableCollection事件,当事件被添加时会向其附加一个PropertyChanged事件,并且该事件侦听器会侦听更改并将其作为需要的。

这是一个例子:(希望语法是正确的,因为我只是通过C#运行它到VB.Net转换器)

Public Sub New()
    AddHandler MyCollection.CollectionChanged, AddressOf MyCollection_CollectionChanged
End Sub

Private Sub MyCollection_CollectionChanged(sender As Object, e As CollectionChangedEventArgs)
    If e.NewItems IsNot Nothing Then
        For Each item As MyItem In e.NewItems
            AddHandler item.PropertyChanged, AddressOf MyItem_PropertyChanged
        Next
    End If

    If e.OldItems IsNot Nothing Then
        For Each item As MyItem In e.OldItems
            RemoveHandler item.PropertyChanged, AddressOf MyItem_PropertyChanged
        Next
    End If
End Sub

Private Sub MyItem_PropertyChanged(sender As Object, e As PropertyChangedEventArgs)
    If e.PropertyName = "Some Property" Then
        DoWork()
    End If
End Sub

C#版本如下所示:

public MyViewModel()
{
    MyCollection.CollectionChanged += MyCollection_CollectionChanged;
}

void MyCollection_CollectionChanged(object sender, CollectionChangedEventArgs e)
{
    if (e.NewItems != null)
        foreach(MyItem item in e.NewItems)
            item.PropertyChanged += MyItem_PropertyChanged;

    if (e.OldItems != null)
        foreach(MyItem item in e.OldItems)
            item.PropertyChanged -= MyItem_PropertyChanged;
}

void MyItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Some Property")
        DoWork();
}