makitocode
makitocode

Reputation: 948

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); }
    }

}

Upvotes: 0

Views: 5846

Answers (4)

makitocode
makitocode

Reputation: 948

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.

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

Use ItemClick event of ListView

//Here sender is your list view
public delegate void ItemClickEventHandler(object sender, ItemClickEventArgs e)
{
   ListView lv = sender as ListView;
   Segment seg = e.ClickedItem as Segment;

   if (lv != null)
   {
      //Use your lv if you want to
   }

   if (seg != null)
   {
      string name = seg. SegmentName;
   }
}

Update

private void ListView_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
    lv.SelectedItem; // If you have set SelectionMode="Single" then it will do
    //the trick but if you set SelectionMode="Multiple" then it's the first
    //selected item

    lv.SelectedItems; // It will give you the selected list of item view models

    e.AddedItems; //This will give you the added items. As it is a list so can
    //be multiple but when it can be multiple I don't know that case.

    e.RemovedItems; //Similarly removed items
}

Upvotes: 5

DJ Burb
DJ Burb

Reputation: 2364

In your view model create a property like this:

    public class ViewModelSegments : BindableBase, INotifyPropertyChanged
    {

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

      //Bind this property to your ListView SelectedItem property in the xaml

      Segment _selectedSegment;
      public Segment SelectedSegment   
      {
        get
        {
           return _selectedSegment;
        }
        set
        {
           _selectedSegment=value;
           NotifyPropertyChanged("SelectedSegment");
        }
      }

      public event PropertyChangedEventHandler PropertyChanged;

      public void NotifyPropertyChanged(String propertyName)
      {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }

   }

In your xaml change your ListView to this:

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

Now whenever your ListView selection changes, the SelectedSegment changes with it, and then you can access the SelectedSegment.SegmentName

Upvotes: 1

Random IT Guy
Random IT Guy

Reputation: 623

Something like this?

        string items = "";
        foreach(ListViewItem item in listView1.SelectedItems){
            items += "Item: " + item.Text + "\n";
            foreach (ListViewItem.ListViewSubItem subitem in item.SubItems)
            {
                items += "SubItem: " + subitem.Text + "\n";
            }
        }
        MessageBox.Show(items);

Upvotes: 0

Related Questions