我正在尝试创建一个列表框来显示页面内容的缩略图视图,用于一个有一个画布但有多个“页面”的应用程序。是的,这可能不是最好的起点,但由于历史原因,这就是我所拥有的。我已经实现了ListBox,并将数据绑定到一个ObservableCollection为PageData的单例'WorkBook‘上(页面上显示的所有内容,包括它的背景)。我真正想要的是能够在ListBoxItem被选中时更改它的边框颜色,并在它的内容是托管集合的类中当前选定的项时保持该边框颜色。
我的问题是:- 1/我无法让ListBox在程序启动时选择第一项。
2/当ListBox失去焦点时,SelectedIndex始终为-1 (因此没有选择)
3/添加到ListBox不会导致选择(SelectedIndex == -1)
4/使用触发器,我可以设置selectedItem的边框,但是当ListBox失去焦点时,这个边框就会丢失。当ListBoxItem显示一个不透明的图像时,当焦点不清晰时,选择颜色的“标准”方式不起作用--即
<Style x:Key="PsThumb">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White"></SolidColorBrush>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="White"></SolidColorBrush>
                </Style.Resources>
</Style>我的ListBox代码如下:
<ListBox  x:Name="PageSorter" Style="{StaticResource PsThumb}"  Width="148" BorderThickness="4" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        VerticalAlignment="Stretch" ItemsSource="{Binding Pages,Source={StaticResource WorkBook}}" SelectedItem="{Binding Path=CurrentPageData, Mode=TwoWay}" 
         AllowDrop="True" ScrollViewer.VerticalScrollBarVisibility="Auto">
                <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                         <Border x:Name="border" BorderBrush="DarkGray" BorderThickness="4" Margin="2,4,2,4" CornerRadius="5">
                             <Border.Effect>
                                 <DropShadowEffect ShadowDepth="6"/>
                             </Border.Effect>
                             <Image Source="{Binding Thumbnail}" Width="130" Height="95" Stretch="Fill"/>
                         </Border>
                    </Grid>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="Red"></Setter>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>发布于 2013-01-22 09:44:56
实现您所要求的最简单的方法是将DataTrigger绑定到ListBox的项内的属性。在下面的示例中,我在Contact类上添加了Selected属性:
public class Contact : INPCBase
{
    public string Name { get; set; }
    private bool _Selected;
    public bool Selected 
    {
        get { return _Selected; }
        set
        {
            _Selected = value;
            NotifyPropertyChanged("Selected");
        }
    }
}然后,我将我的ListBox绑定到List<Contact>。当用户检查CheckBox时,我们更改Selected属性,然后触发Style
<Window x:Class="WpfApp.ListboxKeepSelectionWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ListboxKeepSelectionWindow" Height="277" Width="343"
    xmlns:me="clr-namespace:WpfApp">
<Window.Resources>
    <me:ContactList x:Key="sample"/>
    <Style TargetType="ListBoxItem" x:Key="SelectedListBoxItemStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Selected}" Value="True">
                <Setter Property="BorderBrush" Value="Orange"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <ListBox Name="lbxContacts"
             ItemsSource="{StaticResource ResourceKey=sample}"
             SelectionMode="Extended"
             ItemContainerStyle="{StaticResource ResourceKey=SelectedListBoxItemStyle}" 
             SelectionChanged="lbxContacts_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <CheckBox IsChecked="{Binding Path=Selected}">
                        <TextBlock Text="{Binding Path=Name}"/>
                    </CheckBox>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
为了允许用户在不使用复选框的情况下选择项,我在ListBox上添加了这个小事件。因为Contact类实现了INotifyPropertyChanged接口,所以CheckBox的值被更新,这样用户就知道选择起作用了:
    private void lbxContacts_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        foreach (Contact c in e.AddedItems)
        {
            c.Selected = !c.Selected;
        }
    }当您想要获取选定项的列表时,只需在ItemsSource上使用LINQ查询来获取Selected == true所在位置的项。
https://stackoverflow.com/questions/14428216
复制相似问题