繁体   English   中英

在列表框中的所选项目周围显示边框

[英]Display Border around Selected Item in ListBox

我是一个包含来自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>

MainPage.xaml.cs

 private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //Place border round currently selected image
        //?

    }

有什么想法吗?

更新修复

MainPage.xaml

//within the ListBox DataTemplate
 <Border>
     <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
 </Border>

MainPage.xaml.cs

//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"]);    

我会在您的数据模板中的图像上方创建另一个图像。 该图像几乎是透明的,只有边框,并且可能有一点点滴答声(如Windows 8应用程序所具有的)。 然后,当选中该项目时,我将切换此图像的可见性。

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

我使用这种技术来“灰化”项目(通过使用具有约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>

然后:

 private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //set the visibility property of the selected item to 'Visible'

    }

暂无
暂无

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

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