wcfwindows-phone-7data-bindingcastingwindows-phone-7.1

Cannot get selected listbox


Here is the image I want to extract the enjoymentID

The inner data details I want to extract

I want to extract the enjoymentID from the data bounded selected item, but i tried all the way available in web still failed to extract it due to invalidCastException.

    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        SuperData data = (sender as ListBox).SelectedItem as SuperData;
        ListBoxItem selected = this.listBox1.ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;
    }

I tried this and also e.AddedItem[0] but still could not get it.

<Grid>
                <ListBox HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="listBox1" VerticalAlignment="Stretch" SelectionChanged="listBox1_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Height="132">
                                <Image Source="{Binding image}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
                                <StackPanel Width="370">
                                    <TextBlock Text="{Binding title}" Foreground="#FFC8AB14" FontSize="28" />
                                    <!--TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24" /-->
                                    <TextBlock Text="{Binding description}" TextWrapping="Wrap" FontSize="24" />
                                    <TextBlock Text="Test" TextWrapping="Wrap" FontSize="24" />
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>

This is the UI part where i bind the data. The return from WCF is List<REData> data type and i just bind to the listbox with listBox1.ItemsSource = e.Result.

    public int category { get; set; }
    public int categoryField { get; set; }
    public string description { get; set; }
    public string descriptionField { get; set; }
    public int enjoymentID { get; set; }
    public int enjoymentIDField { get; set; }
    public string image { get; set; }
    public string imageField { get; set; }
    public object PropertyChanged { get; set; } << i don't know what is this
    public string title { get; set; }
    public string titleField { get; set; }

This is the content inside e.AddedItem[0] also:

enter image description here


Solution

  • From your screen shot the type of the first item of the array is RoyalEnjoyment.REServiceReference.REData, if you cast e.AddedItems[0] to this type then you should be able to gain access to the various property values.

    For example, RoyalEnjoyment.REServiceReference.REData x = (RoyalEnjoyment.REServiceReference.REData)e.AddedItems[0];