locked
Change the Stackpanel color when user select an item in the ListView

    Question

  • The StackPanel is used to contain the content.

    I need to change the StackPanel background color for the item selected by user.

    Apprecaite your help.

    here my XAML:


    <DataTemplate x:Key="SpeechTemplate">

     <StackPanel x:Name="Sp" Width="400" Height="120" Background="Red">

        <TextBlock Text="{Binding Title}"
                         Grid.Column="0"
                         Margin="3,3,0,0"
                         TextWrapping="Wrap"
                         VerticalAlignment="Top"
                         FontSize="24"
                         FontWeight="SemiBold"/>

        <TextBlock Text="{Binding SId}"
                         Grid.Column="1"
                         Margin="3,3,0,0"
                         TextWrapping="Wrap"
                         VerticalAlignment="Top"
                         FontSize="18"/>

       <TextBlock Text="{Binding TopicId}"
                          Grid.Column="2"
                          Margin="3,3,0,0"
                          TextWrapping="Wrap"
                          VerticalAlignment="Top"
                          FontSize="18"/>

       </StackPanel>

     </DataTemplate>

    <ListView x:Name="SpeechesGridView" Width="420"                              
                      Foreground="White"    
                      SelectionMode="None"
                      IsSwipeEnabled="True"
                      IsItemClickEnabled="True"

          ItemsSource="{Binding Mode=OneWay, Source={StaticResource SpeechesViewSource}}"                     
          ItemTemplate="{StaticResource SpeechTemplate}"

    Tapped="SpeechesGridView_Tapped"

          ItemClick="SpeechesGridView_ItemClick"     

          SelectionChanged="SpeechesGridView_SelectionChanged"/>

    -- this not working

     private async void SpeechesGridView_Tapped(object sender, TappedRoutedEventArgs e)
            {
             
               ListView Lw = sender as ListView;

               StackPanel stp = Lw.SelectedItem as StackPanel;

               stp.Background = new SolidColorBrush(Windows.UI.Colors.Green);

    }

    Tuesday, November 25, 2014 8:55 AM

Answers

  • Problem is that the SelectedItem is not a stackPanel, but one of the items to which the list is databound.b. When you cast the selecteditem, it won't cast properly into the StackPanel.

    See my video blog on how to capture the proper objects to change the items in the datatemplate:
    http://channel9.msdn.com/Series/Windows-Store-Developer-Solutions/Playing-Hide-and-Go-Seek-with-DataTemplated-Controls


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by FireDance Wednesday, November 26, 2014 1:06 PM
    Tuesday, November 25, 2014 9:14 PM
    Moderator

All replies

  • Problem is that the SelectedItem is not a stackPanel, but one of the items to which the list is databound.b. When you cast the selecteditem, it won't cast properly into the StackPanel.

    See my video blog on how to capture the proper objects to change the items in the datatemplate:
    http://channel9.msdn.com/Series/Windows-Store-Developer-Solutions/Playing-Hide-and-Go-Seek-with-DataTemplated-Controls


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by FireDance Wednesday, November 26, 2014 1:06 PM
    Tuesday, November 25, 2014 9:14 PM
    Moderator
  • Hi Matt,

    Say I have 5 stackpanels in the ListView control.

    and say, the default color is Red for the 5 stackpanels.

    Suppose I can get the user selected current stack panel ( say Stackpanel-1) to change its background color to blue. But when I click the next stackpanel-2 , the stack panel-1 color is blue and the 3 are in red.

    Problem: How to show the ones not selected in their default color?

    I need to show the stackpanel user selected change its color from red to blue. The rest will be in red.

    Thanks

    Wednesday, November 26, 2014 10:39 AM
  • Hi Matt,

    Thank you. Your solution works.

    Wednesday, November 26, 2014 1:06 PM