locked
How to remove selected item of a listview when clicked on another listview of same page and how to retain background color of item selected when tapped/left-clicked? RRS feed

  • Question

  • Hi,

    How should I deal with this? I have two or more ListViews in a page, each ListViewItem can be selected. Depending on what ListView a selected item is contained, a specific command/code will run. I have to remove this selected item on the other ListView so that the user won't be confused. Also, I need to retain the selected item background color of selected ListViewItem when I tap or left click. I know this can be done when right-tapped or right click. But there is separate events when it is tapped/left-clicked or right-tapped/right-clicked. Thank you!

    Monday, December 2, 2013 6:13 AM

Answers

  • Hi Paulo,

    For your first question: How to remove selected item of a listview when clicked on another listview of same page.

    Let's say they are listview1 and listview2, are they using the same data source? Or can you tell me a more detail scenario. If they are using the same ItemsSource, just bind the selected item of listview1 to listview2. If not, you could always fire a SelectionChanged event for helping you identify which item is unselected, also cancel the selection of the item in listview2.

    Question 2: how to retain background color of item selected when tapped/left-clicked

    While the tapped a item, you could get the item from "Windows.UI.Xaml.Input.TappedRoutedEventArgs e" by following statement

    Windows.UI.Xaml.Controls.Primitives.ListViewItemPresenter item = e.OriginalSource as Windows.UI.Xaml.Controls.Primitives.ListViewItemPresenter;
    --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.

    Tuesday, December 3, 2013 3:00 AM
    Moderator
  • Hi Paulo,

    I have some codes for you, I binding the listview1's SelectedItem to listview2's and it works fine. It should also work fine in your situation.

            <ListView x:Name="lv1" Tapped="asd" Margin="915,568,0,0" SelectedItem="{Binding ElementName=lv2,Path=SelectedItem,Mode=TwoWay}">
                <x:String>asdasdasdasd</x:String>
                <x:String>asdasdasdasd11111</x:String>
            </ListView>
            <ListView x:Name="lv2" Margin="157,542,758,26">
                <x:String>asdasdasdasd</x:String>
                <x:String>asdasdasdasd11111</x:String>
            </ListView>
    --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.


    Tuesday, December 3, 2013 5:56 AM
    Moderator

All replies

  • Hi Paulo,

    For your first question: How to remove selected item of a listview when clicked on another listview of same page.

    Let's say they are listview1 and listview2, are they using the same data source? Or can you tell me a more detail scenario. If they are using the same ItemsSource, just bind the selected item of listview1 to listview2. If not, you could always fire a SelectionChanged event for helping you identify which item is unselected, also cancel the selection of the item in listview2.

    Question 2: how to retain background color of item selected when tapped/left-clicked

    While the tapped a item, you could get the item from "Windows.UI.Xaml.Input.TappedRoutedEventArgs e" by following statement

    Windows.UI.Xaml.Controls.Primitives.ListViewItemPresenter item = e.OriginalSource as Windows.UI.Xaml.Controls.Primitives.ListViewItemPresenter;
    --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.

    Tuesday, December 3, 2013 3:00 AM
    Moderator
  • Hi James,

    Thank you for your answers.

    For my first question, below is my xaml code:

    <ListView ItemSource={Binding Schedules}>
     <ListView.ItemTemplate>
      <DataTemplate>
       <StackPanel Orientation="Vertical>
        <!--if there are more than 1 item in Schedules Collection, ListView below will multiply-->
        <ListView ItemSource={Binding Shows}">
         <ListView.ItemTemplate>
          <DataTemplate>
           <TextBlock Text={Binding Time}/>
          </DataTemplate>
         <ListView.ItemTemplate>
        <ListView ItemSource={Binding Shows}">
       <StackPanel Orientation="Vertical>
      <DataTemplate>
     <ListView.ItemTemplate>
    <ListView ItemSource={Binding Schedules}>

    Yes, they have the same DataSource which is Schedules.

    For my 2nd question, Thank you! :)


    Tuesday, December 3, 2013 3:21 AM
  • Hi Paulo,

    I have some codes for you, I binding the listview1's SelectedItem to listview2's and it works fine. It should also work fine in your situation.

            <ListView x:Name="lv1" Tapped="asd" Margin="915,568,0,0" SelectedItem="{Binding ElementName=lv2,Path=SelectedItem,Mode=TwoWay}">
                <x:String>asdasdasdasd</x:String>
                <x:String>asdasdasdasd11111</x:String>
            </ListView>
            <ListView x:Name="lv2" Margin="157,542,758,26">
                <x:String>asdasdasdasd</x:String>
                <x:String>asdasdasdasd11111</x:String>
            </ListView>
    --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.


    Tuesday, December 3, 2013 5:56 AM
    Moderator
  • Hi James,

    I'm having a hard time applying your solution, because my lists are dynamic, so I am not sure if this will be applicable. but thank you for helping me this far.

    Thursday, December 5, 2013 2:46 PM