locked
Unexpected InvalidCastException With DataTemplate

    Question

  • I have the following Xaml for which I set the ItemsSource property as follows:

    <ItemsControl x:Name="itmRecipients" Grid.Column="0" Grid.Row="2" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollMode="Auto" ScrollViewer.BringIntoViewOnFocusChange="True">
    	<ItemsControl.ItemsPanel><ItemsPanelTemplate><StackPanel/></ItemsPanelTemplate></ItemsControl.ItemsPanel>
    	<ItemsControl.ItemTemplate><DataTemplate><RadioButton Style="{StaticResource BasicToggleButton}" GroupName="Recipients" Checked="Recipient_Checked" Content="{Binding Name}" Tag="{Binding Index}"/></DataTemplate></ItemsControl.ItemTemplate>
    </ItemsControl>
    Me.itmRecipients.ItemsSource = Me.Recipients.Select(Function(r, i) New With {.Name = r.Name, .Index = i})

    As you can see, the DataTemplate is just a RadioButton which uses the Name and Index properties of the bound items. The bound items are an anonymous type with the properties Name and Index. But when I run the code, I get the following error:

    A first chance exception of type 'System.InvalidCastException' occurred in GiftTrackerUnivAppTest_Windows.exe

    After looking at App.g.i.vb, I found the following in unhandledExceptionArgs:

    "System.InvalidCastException: Unable to cast object of type 'VB$AnonymousType_0`2[System.String,System.Int32]' to type 'Windows.UI.Xaml.Controls.RadioButton'.
        at GiftTrackerUnivAppTest_Windows.MainPage.MainPage_Loaded(Object sender, RoutedEventArgs e)"

    'VB$AnonymousType_0`2[System.String,System.Int32]' is obviously my anonymous type with the Name and Index properties. Why is it trying to convert my anonymous type to a RadioButton instead of applying it to the DataTemplate? Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, January 04, 2015 8:18 PM

Answers

  • Okay, I found the problem. As it seems like it always is, it was something else and a stupid error on my part. In the line after setting ItemsSource, I had the following line of code:
    If Me.Recipients.Any() Then CType(Me.itmRecipients.Items(Me.SelectedRecipientIndex), RadioButton).IsChecked = True
    As you can see, this line attempts to convert one of the Items (which are, of course, the anonymous types) to a RadioButton. I guess when I was writing the code I forgot that Items are the data, not the DataTemplate. All this time we spend looking at the wrong part trying to find an error that is somewhere else… But thanks for your help anyway!

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, January 05, 2015 5:22 PM

All replies

  • Hi Nathan,

    I used the following code snippet to test in store app, but I worked. I could not reproduce your problem. Can you post a repro project or post more information to explain your scenario?

     

     Private Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
    
            list = New System.Collections.ObjectModel.ObservableCollection(Of Employee)()
    
            Dim rnd As New Random()
    
            For i As Integer = 0 To 99
    
                Dim ee As New Employee()
    
                ee.Name = "herro" & i.ToString()
    
                ee.Index = rnd.Next(0, 101)
    
                list.Add(ee)
    
            Next
    
            itmRecipients.ItemsSource = list.Where(Function(w) w.Index > 10).[Select](Function(s) New With { _
    
                Key .Name = s.Name, _
    
                Key .Index = s.Index _
    
            })
    
    End Sub
    

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, January 05, 2015 7:45 AM
    Moderator
  • Okay, I found the problem. As it seems like it always is, it was something else and a stupid error on my part. In the line after setting ItemsSource, I had the following line of code:
    If Me.Recipients.Any() Then CType(Me.itmRecipients.Items(Me.SelectedRecipientIndex), RadioButton).IsChecked = True
    As you can see, this line attempts to convert one of the Items (which are, of course, the anonymous types) to a RadioButton. I guess when I was writing the code I forgot that Items are the data, not the DataTemplate. All this time we spend looking at the wrong part trying to find an error that is somewhere else… But thanks for your help anyway!

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, January 05, 2015 5:22 PM