简体   繁体   English

如何在LongListSelector中突出显示所选项

[英]How to Highlight a Selected Item in LongListSelector

I would like to simply show a border around the currently selected item in my LongListSelector. 我想在我的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. 我为我的LongListSelector设置了一个ItemTemplate,但我不确定如何修改Border以便只有当前选中的项包含边框。

MainPage.xaml 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. 目前, LongListSelector所有项都显示边框。 I would prefer to modify this in the code behind, but what I have thus far does not work 我宁愿在后面的代码中修改它,但到目前为止我没有工作

MainPage.xaml.cs 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 在WP8上的LongListSelector中突出显示所选项目 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. 当您访问所选项时,您应该将其作为border而不是LongListSelector因为这是您显示每个项目的方式,而LongListSelector是容器。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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