empty property causes update failure

3 posts, 0 answers
  1. David Ocasio
    David Ocasio avatar
    147 posts
    Member since:
    Nov 2009

    Posted 15 Jul 2010 Link to this post

    Not sure if this is a bug (probably not).
    But i am posting it for posterity since it caused me grief.

    If a collection property (observable collection) is nothing when first presented to the itemssource
    any subsequent  assignments are not observed.

    (My data class was generated by a webservice proxy and since it was just a shallow representation of the class the default new assignemnt for the collection was not reproduced in the client version.)

    This can be worked around by making sure all the collections are initailized before assigning to the itemssource or
    by reassinging the itemssource after the addition

    My example below
    click 1st button and a new item is added to a new initialized child collection but it is not displayed in the listview
    click 2nd button and i reassign the who itemssource and it shows up.

    <UserControl x:Class="RadControlsSilverlightApp8.MainPage"
        xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" 
        xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
      
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
      <Grid x:Name="LayoutRoot">
            <StackPanel Orientation="Vertical">
                <Button Content="Add New Level " Height="23" HorizontalAlignment="Left"  Name="Button1" VerticalAlignment="Top"  />
                <Button Content="Reassign" Height="23" HorizontalAlignment="Left"  Name="Button2" VerticalAlignment="Top"  />
                <telerikGridView:RadTreeListView IsReadOnly="True" x:Name="tvKits" Grid.Row="1"  Grid.Column="1" AutoGenerateColumns="False" >
                    <telerikGridView:RadTreeListView.ChildTableDefinitions>
                        <telerikGridView:TreeListViewTableDefinition ItemsSource="{Binding Items}" />                                       
                    </telerikGridView:RadTreeListView.ChildTableDefinitions>
                    <telerikGridView:RadTreeListView.Columns>
                        <telerikGridView:GridViewDataColumn Header="Assembly"  DataMemberBinding="{Binding Name}"  />
                    </telerikGridView:RadTreeListView.Columns>
                </telerikGridView:RadTreeListView>
            </StackPanel>
        </Grid>
    </UserControl>

    Imports System.Collections.ObjectModel
      
    Partial Public Class MainPage
        Inherits UserControl
      
        Dim zz As New ObservableCollection(Of Test)
        Public Sub New()
            InitializeComponent()
        End Sub
      
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
            zz(0).Items(0).Items(0).Items = New ObservableCollection(Of Test)
            zz(0).Items(0).Items(0).Items.Add(New Test)
        End Sub
      
        Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
            tvKits.ItemsSource = Nothing
            tvKits.ItemsSource = zz
        End Sub
      
        Private Sub tvKits_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles tvKits.Loaded
            zz.Add(New Test)
            zz(0).Items = New ObservableCollection(Of Test)
            zz(0).Items.Add(New Test)
      
            zz(0).Items(0).Items = New ObservableCollection(Of Test)
            zz(0).Items(0).Items.Add(New Test)
            tvKits.ItemsSource = zz
            '        tvKits.ExpandAllHierarchyItems()
        End Sub
    End Class
      
    Public Class Test
        Private _Items As ObservableCollection(Of Test)
        Public Property Items() As ObservableCollection(Of Test)
            Get
                Return _Items
            End Get
            Set(ByVal value As ObservableCollection(Of Test))
                _Items = value
            End Set
        End Property
        Private _Name As String = "AA"
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
    End Class

  2. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 16 Jul 2010 Link to this post

    Hello David Ocasio,

    Please add the new items like this:

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        'zz(0).Items(0).Items(0).Items = New ObservableCollection(Of Test)
        zz(0).Items(0).Items(0).Items.Add(New Test)
    End Sub

    Hope this helps.

    Sincerely yours,
    Veskoni
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. David Ocasio
    David Ocasio avatar
    147 posts
    Member since:
    Nov 2009

    Posted 16 Jul 2010 Link to this post

    that is the problem exactly

    the backing store field _Items is defined as

    Private _Items As ObservableCollection(Of Test)

    and so must have the

    zz(0).Items = New ObservableCollection(Of Test) 

    or it will err out since _items is nothing

    As i said the code for the class (test in this example) is not created by me persay but is generated by the web proxy .
    it does not initilize the private member if there isn't any values

    Again I dont consider this a bug, rather I post it as a clue if anyone has the control failing to update its children .
    Check the initialization of the variables.

    thanks
    Veskoni
Back to Top