Search code examples
c#xamlwindows-8windows-store-appswindows-store

How to get the item selected from ListVIew


ok, i hope you understand me...

i have a listView. The ListView is filled with a collection (List lists) of data, but each item within the ListView has the same template (border, grid, border, textblock), so, how do I get the text property of the textblock when I select an item ??

xmlns:vmSegments="using:EC_UsuarioWin8.ViewModels"


<Page.Resources>
   <DataTemplate x:Key="ItemTemplateStyleSegment">
        <Border  Margin="-10,0,0,0" Background="White" >
            <Grid Height="120" Width="200">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Border Grid.Row="0">
                    <Image Source="{Binding SegmentImage}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10"/>
                </Border>
                <Border Grid.Row="1" >
                    <TextBlock Text="{Binding SegmentName}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FFE0241B" FontSize="20" />
                </Border>
            </Grid>
        </Border>
    </DataTemplate>
</Page.Resources>

<Grid>
   <Grid.Resources>
                <vmSegments:ViewModelSegments x:Key="SegmentCollectionVM" x:Name="SegmentCollectionVM"/>
   </Grid.Resources>

<ListView x:Name="ListVIewSegments" ItemsSource="{Binding Source={StaticResource SegmentCollectionVM}, Path= SegmentList}" Grid.Row="1" Margin="20" ItemContainerStyle="{StaticResource ListViewSItemtyleECFood}" ItemTemplate="{StaticResource ItemTemplateStyleSegment}" SelectionChanged="ListViewSegments_SelectionChanged">
</ListView>

</Grid>

My Class ViewModelSegments:

public class ViewModelSegments : BindableBase
{

    private SegmentsCollection _segmentList = new SegmentsCollection();
    public SegmentsCollection SegmentList
    {
        get { return _segmentList; }
        set { SetProperty(ref _segmentList, value); }
    }
}

My class SegmentsCollection:

public class SegmentsCollection: ObservableCollection<Segment>
{

}

My Class Segment:

public class Segment : BindableBase
{
    //Propiedad SegmentName
    private string Name;
    public string SegmentName
    {
        get { return Name; }
        set { SetProperty(ref Name, value); }
    }

    //Propiedad SegmentPicture
    private Uri image;
    public Uri SegmentImage
    {
        get { return image; }
        set { SetProperty(ref image, value); }
    }

}

Solution

  • thks to Inder Kumar Rathore and DJ Burb to guide me to find the answer. so, here is

    Segment seg= ListView.SelectedItem as Segment;
    if (seg!= null)
    {
        tbckChoiceSelected.Text = compa.CompanyName;//TextBlock called bckChoiceSelected
    }
    

    it was very easy. again, thnks.