简体   繁体   中英

How to Highlight a Selected Item in LongListSelector

I would like to simply show a border around the currently selected item in my LongListSelector. I have set an ItemTemplate for my LongListSelector, but I am unsure of how to modify the Border so that only the currently selected item contains a border.

MainPage.xaml

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <!-- BorderBrush of all items are currently set to PhoneAccentBrush, need only currently selected item! -->
        <Border x:Name="brd" CornerRadius="10" BorderBrush="{StaticResource PhoneAccentBrush}" Width="Auto" BorderThickness="3">
            <Viewbox Width="108" Height="108">
                <Image x:Name="recentImage" Source="{Binding Source}" Margin="6,6" Width="108"/>
            </Viewbox>
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu x:Name="imgListContextMenu" Background="{StaticResource PhoneChromeBrush}">
                    <toolkit:MenuItem Foreground="{StaticResource PhoneForegroundBrush}" Header="delete" Click="deleteContextMenuItem_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
        </Border>
    </DataTemplate>

</phone:PhoneApplicationPage.Resources>

...

<phone:LongListSelector x:Name="Recent" Margin="0" 
                                    SelectionChanged="recent_SelectionChanged" 
                                    toolkit:TiltEffect.IsTiltEnabled="True"
                                    LayoutMode="Grid" GridCellSize="108,108"
                                    ItemTemplate="{StaticResource ItemTemplate}"
                                    />

Currently all of the items within the LongListSelector show the border. I would prefer to modify this in the code behind, but what I have thus far does not work

MainPage.xaml.cs

private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {            
        var item = sender  as LongListSelector
        item.BorderBrush = App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
    }

Any ideas?

Refer this link ,

Highlight a selected item in the LongListSelector on WP8 http://code.msdn.microsoft.com/wpapps/Highlight-a-selected-item-30ced444

When you access the selected item, you should access it as a border and not as a LongListSelector because that's how you show each item, while the LongListSelector is the container. You also forgot a semi-colon on the 3rd row, I've added it for you.

Your new code would be:

private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e)
{            
    var item = sender as Border;
    item.BorderBrush = App.Current
                          .Resources["PhoneAccentBrush"] as SolidColorBrush;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM