locked
How get the ListView's SelectedIndex?

    Question

  • Hello,
    sorry but i'm a bit new in programming Windows Store Apps. It's all different from Windows Forms and Visual Basic .Net i knew before years.

    I have a ListView and want to get the SelectedItem or SelectedIndex when i click a button outside of the ListView. I searched on MSDN or Google but all the examples i found do not compile. The types are not found or it does nothing.

    What should i do here:

    <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
      <Page.DataContext> 
        <local:clsTextVM/>
      </Page.DataContext>
    
      <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView x:Name="List" HorizontalAlignment="Left" Height="648" Margin="25,40,0,0" VerticalAlignment="Top" Width="1331"
                  ItemsSource="{Binding CustomObj_List, Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Auto" 
                  SelectionMode="Single">
          <ListView.ItemTemplate>
            <DataTemplate>
              <StackPanel Orientation="Horizontal">
                <TextBox Text="{Binding Name, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" MinWidth="120" FontSize="18"/>
                <TextBox Text="{Binding Description, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap"  MinWidth="180" Margin="10,0,0,0" FontSize="18"/>
                <TextBox Text="{Binding Remarks, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" MinWidth="1000" Margin="10,0,0,0" FontSize="18"/>
                </StackPanel>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
        <TextBlock Text="{Binding CustomObj_List_File_Name}" FontSize="18" Margin="25,729,0,0"/>
        <Button x:Uid="New" 
                Command="{Binding CustomObj_List_New_RelayCommand}"
                HorizontalAlignment="Left" Margin="1078,729,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.382,1.944"/>
        <Button x:Uid="Save" 
                Command="{Binding CustomObj_List_Save_RelayCommand}"            
                HorizontalAlignment="Left" Margin="1141,729,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.382,1.944"/>
        <Button x:Uid="invokePrintingButton"  Content="Drucken" Click="Print_Button_Click"
        	      HorizontalAlignment="Left" Margin="1244,729,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.382,1.944"/>
        <Button x:Uid="Open"
                Command="{Binding CustomObj_List_File_Change_RelayCommand}"
               	HorizontalAlignment="Left" Margin="1334,729,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.382,1.944"/>
          </Grid>
    </Page>
    

    Thursday, March 19, 2015 6:50 AM

Answers

  • Have found a solution. Searched on google and someone writes that when inside a list control are other controls, like the textboxes in this case, the textboxes have to handle the event. I have choose GotFocus and it is called.

    The sender is the TextBox. The TextBox.DataContext object is the clsText object. Then i have to set List.SelectedValue with code.

    Seems that this is necessary when there are textboxes inside a listview with DataBinding. Seems that otherwise the SelectedIndex and SelectedValue property remains -1 and Nothing.

    <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBox Text="{Binding Name, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" GotFocus="TextBox_GotFocus" MinWidth="120" FontSize="18"/> <TextBox Text="{Binding Description, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" GotFocus="TextBox_GotFocus" MinWidth="180" Margin="10,0,0,0" FontSize="18"/> <TextBox Text="{Binding Remarks, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" GotFocus="TextBox_GotFocus" MinWidth="1000" Margin="10,0,0,0" FontSize="18"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate


      Private Sub TextBox_GotFocus(sender As Object, e As RoutedEventArgs)

        Dim objText As clsText = DirectCast(DirectCast(sender, TextBox).DataContext, clsText)
        Dim strDescription As String = objText.Description

        List.SelectedValue = objText
      End Sub


    Thursday, March 19, 2015 4:02 PM

All replies

  • add itemclick event (code behind) to listview and inside that add code like this

    //Listview item click event handler ()
    {
       var selectedItem = e.ClickedItem as YourModelClass;
       //here variable selectedItem as complete value of the selected item from the listview, and you manipulate with them	
    }
    

    Thursday, March 19, 2015 8:48 AM
  • Hello,

    i have tried this  The Event is not called!?

    <ListView x:Name="List" HorizontalAlignment="Left" Height="648" Margin="25,40,0,0" VerticalAlignment="Top" Width="1331"
                  ItemsSource="{Binding CustomObj_List, Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Auto" 
                  SelectionMode="Single" ItemClick="List_ItemClick">
       ...
    
     Private Sub List_ItemClick(sender As Object, e As ItemClickEventArgs) Handles List.ItemClick
        List.SelectedItem = e.ClickedItem
     
        Dim objText As clsText = DirectCast(e.ClickedItem, clsText)
        Dim strRemarks = objText.Remarks
      End Sub
    

    Thursday, March 19, 2015 9:19 AM
  • Bind the SelectedItem property of the ListView to a source property of your clsTextVM view model:

     <ListView x:Name="List" HorizontalAlignment="Left" Height="648" Margin="25,40,0,0" VerticalAlignment="Top" Width="1331"
                  ItemsSource="{Binding CustomObj_List, Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Auto" 
                  SelectionMode="Single" SelectedItem="{Binding SelectedObject}">

    clsTextVM:

    Private selObj As CustomObj
    Public Property SelectedObject() As CustomObj
            Get 
                Return Me.selObj
            End Get 
    
            Set(ByVal value As CustomObj)
                    Me.selObj = value
      'NotifyPropertyChanged()
                End If 
            End Set 
        End Property 
    

    You can then just access the source property CustomObj from any of the commands that gets invoked when the buttons are clicked.

    The other (non-MVVM) approach is to handle the Click event for the button and access the SelectedItem property of the ListView:

        Private Sub btn_Click(sender As Object, e As RoutedEventArgs)
            Dim item As CustomObj = CType(lstViewConditionItems.SelectedItem, CustomObj)
        End Sub

    <Button ... Click="btn_Click" />


    Of course you must change "CustomObj" to the actual name of the type (class) of the items in the CustomObj_List collection.

    Hope that helps.


    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

     

    Thursday, March 19, 2015 10:05 AM
  • Have tried the following. Nothing happens. The Breakpoint in the Property Set is not reached.

    The Base Class of clsTextVM is clsCustomClassVM_Base. clsText is given to it with the type parameter clsCustomClass_Base_TP of type clsCustom_Class_Base with the new constraint. clsText inherits from clsCustom_Class_Base.

    The second approach doesn't work either. I never get the List.SelectedIndex or List.SelectedItem. It is always -1 or Nothing. Other things like creating items, storing in a file, printing all is working. But i don't get the List.Selection when i click a line? What is wrong or what is my mistake?

    MainPage.xaml:
    
      <ListView x:Name="List" HorizontalAlignment="Left" Height="648" Margin="25,40,0,0" VerticalAlignment="Top" Width="1331"
                   ItemsSource="{Binding CustomObj_List, Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Auto" 
                   SelectionMode="Single" SelectedItem="Binding Custom_Obj_List_Selected_Object">
        <ListView.ItemTemplate>
    
    
    clsCustomClassVM_Base: 
    
      Dim mobj_Custom_Obj_List_Selected_Object As clsCustomClass_Base_TP
    
      Public Property Custom_Obj_List_Selected_Object As clsCustomClass_Base_TP
         Get
           Return mobj_Custom_Obj_List_Selected_Object
         End Get
    
        Set(value As clsCustomClass_Base_TP)
           mobj_Custom_Obj_List_Selected_Object = value
    
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Custom_Obj_List_Selected_Object"))
         End Set
       End Property
    
    
    



    • Edited by Markus222 Thursday, March 19, 2015 11:54 AM
    Thursday, March 19, 2015 11:52 AM
  • In a Little test Project it works fine. But not in the other above.

    <ListView Name="List1" HorizontalAlignment="Left" Height="648" Margin="25,40,0,0" VerticalAlignment="Top" Width="1331" SelectionMode="Single" SelectionChanged="Selection_Changed">
          <TextBlock Text="1 Test"/>
          <TextBlock Text="2 Test"/>
          <TextBlock Text="3 Test"/>                 
    </ListView>
    
    Public NotInheritable Class MainPage
        Inherits Page
     
    Private Sub Selection_Changed(sender As Object, e As SelectionChangedEventArgs)
        Dim i As Integer = List1.SelectedIndex
      End Sub
    End Class

    Thursday, March 19, 2015 1:02 PM
  • Have found a solution. Searched on google and someone writes that when inside a list control are other controls, like the textboxes in this case, the textboxes have to handle the event. I have choose GotFocus and it is called.

    The sender is the TextBox. The TextBox.DataContext object is the clsText object. Then i have to set List.SelectedValue with code.

    Seems that this is necessary when there are textboxes inside a listview with DataBinding. Seems that otherwise the SelectedIndex and SelectedValue property remains -1 and Nothing.

    <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBox Text="{Binding Name, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" GotFocus="TextBox_GotFocus" MinWidth="120" FontSize="18"/> <TextBox Text="{Binding Description, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" GotFocus="TextBox_GotFocus" MinWidth="180" Margin="10,0,0,0" FontSize="18"/> <TextBox Text="{Binding Remarks, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" GotFocus="TextBox_GotFocus" MinWidth="1000" Margin="10,0,0,0" FontSize="18"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate


      Private Sub TextBox_GotFocus(sender As Object, e As RoutedEventArgs)

        Dim objText As clsText = DirectCast(DirectCast(sender, TextBox).DataContext, clsText)
        Dim strDescription As String = objText.Description

        List.SelectedValue = objText
      End Sub


    Thursday, March 19, 2015 4:02 PM