Click here to Skip to main content
15,883,127 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm almost there, but not completely :
i have a listview binded to an observablecollection. I want the UI updated after i change the collection, and it does not work now. basically i have a listview and a button. After pressing the button a new item is added to the collection, but the UI stays the same. can someone points me to the right direction ?

thanks.

XAML :
XML
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:StructureItems.SList"
    x:Class="MainWindow"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
        <c:ItemList x:Key="SList"/>
    </Window.Resources>
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="274,17,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <ListView Name="LV" HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="229" ItemsSource="{StaticResource SList}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Code" DisplayMemberBinding="{Binding Code}" Width="50"/>
                    <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>


Classes
VB
Namespace SList

    Public Class ItemList
        Inherits ObservableCollection(Of StructureItem)

        Public Sub New()
            MyBase.Add(New StructureItem("A01", "Arcade"))
            MyBase.Add(New StructureItem("A02", "Autentica"))
            MyBase.Add(New StructureItem("A03", "Belface"))
            MyBase.Add(New StructureItem("A04", "Heering"))
        End Sub

        Public Overloads Sub Add(code As String, description As String)
            MyBase.Add(New StructureItem(code, description))
        End Sub
    End Class

    Public Class StructureItem
        Implements INotifyPropertyChanged

        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

        Private _code As String
        Private _description As String

        Public Sub New(ByVal first As String, ByVal last As String)
            Me._code = first
            Me._description = last
        End Sub

        Public Property Code() As String
            Get
                Return Me._code
            End Get
            Set(ByVal value As String)
                Me._code = value
                OnPropertyChanged("Code")
            End Set
        End Property

        Public Property Description() As String
            Get
                Return Me._description
            End Get
            Set(ByVal value As String)
                Me._description = value
                OnPropertyChanged("Description")
            End Set
        End Property

        Protected Sub OnPropertyChanged(ByVal name As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
        End Sub
    End Class
End Namespace


code behind
VB
Imports StructureItems.SList
Class MainWindow
    Dim _item As New ItemList
    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        _item.Add("A05", "Updated_")
    End Sub
End Class
Posted
Updated 9-Jan-14 3:34am
v2

1 solution

You need to add a constructor to your mainwindow, and then in the constructor you need to set the DataContext :

VB
Me.DataContext = Me

Or something like that. I am a C# programmer and have forgotten VB.
 
Share this answer
 
Comments
bedrb 9-Jan-14 9:58am    
sorry, does not seems to work...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900