c# - 如何突出显示 LongListSelector 中的选定项

标签 c# xaml windows-phone-7 windows-phone-8 longlistselector

我只想在我的 LongListSelector 中的当前选定项周围显示一个边框。我已经为我的 LongListSelector 设置了一个 ItemTemplate,但是我不确定如何修改 Border 以便只有当前选择的项目包含一个边框。

主页.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}"
                                    />

目前 LongListSelector 中的所有项目都显示边框。我更愿意在后面的代码中修改它,但到目前为止我所拥有的不起作用

MainPage.xaml.cs

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

有什么想法吗?

最佳答案

当您访问所选项目时,您应该将其作为 border 而不是 LongListSelector 来访问,因为这是显示每个项目的方式,而 LongListSelector 是容器。您还忘记了第 3 行的分号,我已经为您添加了。

您的新代码将是:

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

关于c# - 如何突出显示 LongListSelector 中的选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18325889/

相关文章:

c# - 几个 C# 语言问题

c# - 如何确定 Web 应用程序中的客户端 .NET 框架版本?

c# - Entity Framework : many to many relationship tables

c# - Xamarin - Prism : How do I pass the whole ViewModel of the ContentPage to a child ContentView

c# - 如何获取 Generic.Xaml 中自定义控件的事件处理程序

javascript - 如何使用插件在 Windows Phone 中的 PhoneGap 中显示加载对话框?

双数的 C# 问题

c# - 按Enter时将Focus设置为DataGridTemplateColumn子控件

windows-phone-7 - 处理 ViewModel 中的 OnNavigatedFrom/OnNavigatedTo 事件

xaml - Windows Phone 7.1 中滚动查看器内的网格