2013-08-20 151 views
3

我想在我的LongListSelector中简单地显示当前选定项目的边框。我为我的LongListSelector设置了一个ItemTemplate,但我不确定如何修改边框,以便只有当前选定的项目包含边框。如何突出显示LongListSelector中的选定项目

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}" 
            /> 

目前所有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; 
    } 

任何想法?

回答

2

请参阅此链接,

高亮显示所选的项目,LongListSelector上WP8 http://code.msdn.microsoft.com/wpapps/Highlight-a-selected-item-30ced444

+0

出于某种原因,我无法将其适用于我的解决方案。 – Matthew

+0

@Mthethew它为我工作得很好。我在选择时采用了同样的突出显示项目,因为我在后面的代码中设置所选项目时遇到了麻烦。这对你来说是一个问题吗? –

+1

我其实去了一个略有不同的解决方案http://stackoverflow.com/questions/18436328/how-to-create-border-around-longlistselector-selecteditem/18438027?noredirect=1#18438027,我认为你会感兴趣在检查。我会将你的标记标记为正确的,因为我知道它也适用。我很感激! – Matthew

-1

当您访问所选项目时,您应该将其作为border而非LongListSelector进行访问,因为这是您显示每个项目的方式,而LongListSelector是容器。你也在第三排忘记了一个分号,我已经为你添加了它。

您的新代码将是:

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

谢谢,其实我已试过为好。我在最后一行得到一个'AccessViolationException' – Matthew

+0

此外,我从xaml的'Border'标签的第一行删除了'BorderBrush'属性'默认情况下,所有项目都设置为”PhoneAccentBrush“的边框颜色(我只是在那里测试并确保设置边框颜色) 。 – Matthew

+1

这根本不起作用。 –

相关问题