locked
Listview with StackPanel selected item RRS feed

  • Question

  • How can I go about getting the contents of the selected row of the listview in the code below.

    Thanks in advance.

    <Grid x:Name="LICGRID" Margin="50,412,51,10" Grid.Row="1" HorizontalAlignment="Stretch">
                <ListView Name="listviewlic" ItemsSource="{Binding}" HorizontalAlignment="Stretch" SelectedItem="{Binding lgridstackpanel}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel x:Name="lgridstackpanel" Orientation="Horizontal" >
                                <TextBlock Text="{Binding LicenseCount}" Width="150" HorizontalAlignment="Left" FontFamily="Global User Interface"></TextBlock>
                                <TextBlock Text="{Binding title}"  Width="250" HorizontalAlignment="Center" TextAlignment="Left"></TextBlock>
                                <TextBlock Text="{Binding LicenseKey}"  Width="300" HorizontalAlignment="Left" FontFamily="Global User Interface"></TextBlock>
                                <TextBlock Text="{Binding LicenseInvoice}"  HorizontalAlignment="Left" Width="200"></TextBlock>
                                <TextBlock Text=""  HorizontalAlignment="Left" Width="200"></TextBlock>
                                <TextBlock Text=""  HorizontalAlignment="Left" FontFamily="Global User Interface" ></TextBlock>
                            </StackPanel>
    
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>

    Tuesday, February 4, 2014 7:54 PM

Answers

  • I got a screenshot for you while your SelectionChanged event fired, by using

    var selected = e.AddedItems[0] as StackPanel

    you should be able to get your StackPanel.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, February 5, 2014 6:47 AM
    Moderator
  • Hello Ronald

    It is really easy to access that. You should use a SelectionChanged event. 

    private void listviewlic_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ListView listView = sender as ListView;
                
                if(listView != null && listView.SelectedItem != null)
                {
                    YourClass yourObject = listView.SelectedItem as YourClass;
                }
            }

    Hope it helps you.

    Arafat

    Wednesday, February 5, 2014 10:08 AM

All replies

  • Hi Ronald,

    There is a SelectionChanged event for you to get the item while the listview selection changed.

    Or you can use SelectedItems or SelectedItem or SelectedIndex to get the current selection item.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, February 5, 2014 1:29 AM
    Moderator
  • I can see the info from selecteditem, but don't know how to get one of those values.
    Wednesday, February 5, 2014 1:41 AM
  • I got a screenshot for you while your SelectionChanged event fired, by using

    var selected = e.AddedItems[0] as StackPanel

    you should be able to get your StackPanel.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, February 5, 2014 6:47 AM
    Moderator
  • Hello Ronald

    It is really easy to access that. You should use a SelectionChanged event. 

    private void listviewlic_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ListView listView = sender as ListView;
                
                if(listView != null && listView.SelectedItem != null)
                {
                    YourClass yourObject = listView.SelectedItem as YourClass;
                }
            }

    Hope it helps you.

    Arafat

    Wednesday, February 5, 2014 10:08 AM