Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to cast object of type 'RuntimePropertyInfo' to type 'ListBoxItem'
    primarykey
    data
    text
    <p>I have this strange problem where I am unable to get an item from a ListBox. I have even tried to use the code from <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx" rel="nofollow">this site</a> but it fails in my situation with a message: <em>Unable to cast object of type 'System.Reflection.RuntimePropertyInfo' to type 'System.Windows.Controls.ListBoxItem'.</em> The ListBox is binded to colors from XAML.</p> <pre class="lang-xaml prettyprint-override"><code>xmlns:sys="clr-namespace:System;assembly=mscorlib" &lt;Window.Resources&gt; &lt;ObjectDataProvider MethodName="GetType" ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp"&gt; &lt;ObjectDataProvider.MethodParameters&gt; &lt;sys:String&gt;System.Windows.Media.Colors, PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 &lt;/sys:String&gt; &lt;/ObjectDataProvider.MethodParameters&gt; &lt;/ObjectDataProvider&gt; &lt;ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}" MethodName="GetProperties" x:Key="colorPropertiesOdp"&gt; &lt;/ObjectDataProvider&gt; &lt;/Window.Resources&gt; &lt;!-- etc --&gt; &lt;ListBox x:Name="ListBoxColor" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="5" Grid.RowSpan="5" SelectedIndex="113"&gt; &lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel Orientation="Vertical" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Rectangle Fill="{Binding Name}" Stroke="Black" Margin="2" StrokeThickness="1" Height="20" Width="50"/&gt; &lt;Label Content="{Binding Name}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <pre class="lang-vb prettyprint-override"><code>Private Sub ListBoxColor_SelectionChanged(sender As Object, _ e As SelectionChangedEventArgs) Handles ListBoxColor.SelectionChanged Dim lbsender As ListBox Dim li As ListBoxItem lbsender = CType(sender, ListBox) li = CType(lbsender.SelectedItem, ListBoxItem) </code></pre> <p>It breaks on the last line. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload