WPF ListBox选择问题,向用户提供反馈

时间:2013-01-20 19:14:23

标签: wpf listbox selection

我正在尝试创建一个列表框,显示页面内容的缩略图视图,用于具有一个画布但多个“页面”的应用程序。是的,这可能不是最好的起点,但由于历史原因,这就是我所拥有的。 我已经实现了ListBox,其数据绑定到单个'WorkBook',它具有PageData的ObservableCollection(页面上显示的所有内容,包括它的背景)。 我真正想要的是能够在选择时更改ListBoxItem的Border颜色并保留Border颜色,而它的内容是托管该集合的类中当前选定的项目。

我的问题是: - 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>

1 个答案:

答案 0 :(得分:0)

实现您所要求的最简单方法是将DataTrigger绑定到ListBox项内的属性。在下面的示例中,我在Selected类中添加了Contact属性:

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的项目。