C# 在列表框中显示选定项周围的边框

C# 在列表框中显示选定项周围的边框,c#,windows-phone-7,listbox,windows-phone-8,C#,Windows Phone 7,Listbox,Windows Phone 8,我是一个列表框,其中包含来自IsolatedStorage的图像,用户可以在其中选择要使用的图像。我想通过图像周围的边框(或者更好的方式)向用户显示他们当前在列表框中选择或按下的图像。我不确定如何获得当前选定的图像并在该图像周围放置边框。我目前正在使用ListBox的SelectionChanged事件尝试此功能。到目前为止,我掌握的情况如下: MainPage.xaml <ListBox x:Name="Recent" ItemsSource="{Binding Pictures}"

我是一个列表框,其中包含来自IsolatedStorage的图像,用户可以在其中选择要使用的图像。我想通过图像周围的边框(或者更好的方式)向用户显示他们当前在列表框中选择或按下的图像。我不确定如何获得当前选定的图像并在该图像周围放置边框。我目前正在使用ListBox的SelectionChanged事件尝试此功能。到目前为止,我掌握的情况如下:

MainPage.xaml

 <ListBox x:Name="Recent" ItemsSource="{Binding Pictures}" Margin="8" 
                     SelectionChanged="recent_SelectionChanged" toolkit:TiltEffect.IsTiltEnabled="True">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
</ListBox>
//within the ListBox DataTemplate
 <Border>
     <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
 </Border>

有什么想法吗?

我会在数据模板中的图像上方创建另一个图像。这张图片大部分是透明的,只有边框,可能还有一点滴答声(就像Windows8应用程序一样)。然后,我会在选择项目时切换此图像的可见性

因为图像大部分是透明的,所以它将显示为选定图像周围的边框

我使用这种技术来“灰显”项目(通过使用一个10%不透明度的纯黑色图像),效果非常好

您应该能够简单地将可见性绑定到所选属性,尽管您需要一个转换器来在布尔值和可见性之间进行转换

  <DataTemplate>
       <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
       <Image x:Name="borderImage" Source="Images/borderimg.png" Margin="12" Width="115" Visibility="Collapsed"/>
  </DataTemplate>
更新以修复

MainPage.xaml

 <ListBox x:Name="Recent" ItemsSource="{Binding Pictures}" Margin="8" 
                     SelectionChanged="recent_SelectionChanged" toolkit:TiltEffect.IsTiltEnabled="True">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
</ListBox>
//within the ListBox DataTemplate
 <Border>
     <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
 </Border>

谢谢你的意见。我刚刚找到了另一种在我的列表框中实际设置所选图像边框的方法。如果你也希望看到我的实现,我已经添加了我的答案。是的,你的方式可能更适合你正在做的事情。如果你想要更复杂一点的东西,我的方式很好-例如,我喜欢Windows8在边框的下角加上一个小三角形,并加上一个小记号的方式。
//within recent_SelectionChanged
var lb = sender as ListBox;
var lbi = lb.ItemContainerGenerator.ContainerFromItem(lb.SelectedItem) as ListBoxItem;

lbi.BorderThickness = new Thickness(2, 2, 2, 2);
lbi.BorderBrush = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);