简体   繁体   English

仅在列表框中突出显示当前选中的项目

[英]Only highlight currently selected item in ListBox

I have a ListBox being populated with images from isolated storage. 我有一个列表框,其中装有来自隔离存储中的图像。 The user is allowed to select the image to perform some action upon it. 允许用户选择图像以对其执行一些操作。 To notify the user of the currently selected image in the list, I simply have placed a border around the selected item. 为了通知用户列表中当前选择的图像,我只是在选择的项目周围放置了边框。 However, when a new image is selected in the list, the border is placed around that image as well, so now both images have a border. 但是,当在列表中选择新图像时,边框也将围绕该图像放置,因此现在两个图像都具有边框。 I would like to find a way to remove the previously selected image's border so that only the currently selected image is highlighted. 我想找到一种方法来删除以前选择的图像的边框,以便仅突出显示当前选择的图像。

What I have so far is as follows: 到目前为止,我所拥有的如下:

MainPage.xaml 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>                            
                        <Border>
                            <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
</ListBox>

MainPage.xaml.cs MainPage.xaml.cs

private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //Place border round currently selected image
        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"]);

        //Where and how to remove border from previously selected image?
    }

So, I am not sure of what exactly to do to accomplish this. 因此,我不确定要执行什么操作。 How might I detect the previously selected image item in the ListBox, or determine which item has the border and remove it before adding the border to the currently selected item? 在将边框添加到当前所选项目之前,如何检测列表框中先前选择的图像项目,或者确定具有边框的项目并将其删除? Any thoughts or references? 有什么想法或参考吗?

You need just edit ItemContainer style of your ListBox . 您只需要编辑ListBox ItemContainer样式。
Like this: 像这样:

<phone:PhoneApplicationPage.Resources>
<Style x:Key="MyStyle" TargetType="ListBoxItem">
  <Setter Property="Background" Value="Transparent"/>
  <Setter Property="BorderThickness" Value="0" />
  <Setter Property="Padding" Value="0" />
  <Setter Property="HorizontalContentAlignment" Value="Left"/>
  <Setter Property="VerticalContentAlignment" Value="Top"/>
  <Setter Property ="Foreground" Value="White" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ListBoxItem">
        <Border x:Name="LayoutRoot" Background="{TemplateBinding Background}" 
                HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                VerticalAlignment="{TemplateBinding VerticalAlignment}" 
                BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}">
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
               <VisualState x:Name="Normal"/>
              <VisualState x:Name="MouseOver" />
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                  </ObjectAnimationUsingKeyFrames>
                  <DoubleAnimation Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Opacity" Duration="0" To=".5" />
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="SelectionStates">
              <VisualState x:Name="Unselected"/>
              <VisualState x:Name="Selected">
                  <Storyboard>
                       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="brd"
                                    Storyboard.TargetProperty="BorderThickness">
                          <DiscreteObjectKeyFrame KeyTime="0" Value="2" />
                      </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
                    <Border x:Name="brd" CornerRadius="10" BorderBrush="White" Width="Auto" BorderThickness="{TemplateBinding BorderThickness}">
                         <Image x:Name="recentImage" Source="{Binding Source}" Margin="12" Width="115"/>
                    </Border>
                </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
</phone:PhoneApplicationPage.Resources>

And your ListBox will be: 您的ListBox将是:

<ListBox x:Name="Recent" ItemsSource="{Binding Pictures}" Margin="8" 
                 SelectionChanged="recent_SelectionChanged" toolkit:TiltEffect.IsTiltEnabled="True" ItemContainerStyle="{StaticResource MyStyle}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

Instead of highlighting selected item from code behind, why not set style for selected item in client side. 为什么不在客户端为所选项目设置样式,而不是从后面的代码中突出显示所选项目。

What I mean to say is, you can use <Style.Triggers> to set the style for selected item in your listbox. 我的意思是,您可以使用<Style.Triggers>设置列表框中所选项目的样式。 (Assuming only one item can be selected at a time) (假设一次只能选择一项)

Sample code- (this sets background of selected item to white . You can apply your style here) 示例代码-(将所选项目的背景设置为白色。您可以在此处应用样式)

  <Style x:Name="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="Selector.IsSelected" Value="True">
                <Setter Property="Background" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>

You may do something like this. 您可以做这样的事情。 (This is just a skeleton. you will have to implement the class properly) (这只是一个框架。您将必须正确实现该类)

    public class MyImage
    {
        public string ImagePath { get; set; }
        public bool IsSelected { get; set; }
    }

Set a collection of this class as the source of your listbox. 将此类的集合设置为列表框的源。 When you select an item Mark 选择项目标记时

     IsSelected=true; 

Remember this property should be "false" for all unselected items. 请记住,对于所有未选中的项目,此属性应为“ false”。

Now your borders visibility can be set based on IsSelected property.Hope this helps. 现在可以基于IsSelected属性设置边框的可见性。希望这会有所帮助。

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

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