列表视图中的选定项目不会取消选中

时间:2011-04-21 08:13:45

标签: wpf listview mvvm selection

我可以在列表视图中选择多个项目。但如果我点击一个,它就会变成蓝色。这是正常的,因此显示它已被选中。但如果我再次点击相同的项目,它不会取消选中。所以我不能改变我的选择。有谁知道如何解决这个愚蠢的小问题?

编辑:这是我的列表视图:

 <ListView Height="155" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible"  SelectedItem="{Binding Path=SelectedQuestionDropList, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" dd:DragDrop.IsDragSource="True" 
  dd:DragDrop.IsDropTarget="True" SelectionMode="Multiple" Margin="0,0,542,436" Background="#CDC5CBC5"
                 dd:DragDrop.DropHandler="{Binding}" Name="DropListView"  ItemsSource="{Binding Path=SelectedExaminationQuestions,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" SelectionChanged="ListView_SelectionChanged_1" VerticalAlignment="Bottom">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Verkorte naam" Width="Auto" DisplayMemberBinding="{Binding Path=ShortName}" />
                        <GridViewColumn Header="Omschrijving" Width="Auto" DisplayMemberBinding="{Binding Path=Description}" />
                        <GridViewColumn Header="Type" Width="Auto" DisplayMemberBinding="{Binding Path=Type}" />

                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>

2 个答案:

答案 0 :(得分:5)

我遇到了类似的问题,发现左键单击总是选择指向的项目,您可以使用Ctrl +左键单击在列表视图中切换选择。这是默认行为。

答案 1 :(得分:3)

您可以编写wpf行为。类似的东西:

public class ListViewBehaviour
{
    /// <summary>
    /// Enfoca automaticament el item sel·leccionat
    /// </summary>
    public static readonly DependencyProperty AutoUnselectItemProperty =
        DependencyProperty.RegisterAttached(
            "AutoUnselect",
            typeof(bool),
            typeof(ListViewBehaviour),
            new UIPropertyMetadata(false, OnAutoUnselectItemChanged));

    public static bool GetAutoUnselectItem(ListView listBox)
    {
        return (bool)listBox.GetValue(AutoUnselectItemProperty);
    }

    public static void SetAutoUnselectItem(ListView listBox, bool value)
    {
        listBox.SetValue(AutoUnselectItemProperty, value);
    }

    private static void OnAutoUnselectItemChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        var listView = source as ListView;
        if (listView == null)
            return;

        if (e.NewValue is bool == false)
            listView.SelectionChanged -= OnSelectionChanged;
        else
            listView.SelectionChanged += OnSelectionChanged;
    }

    private static void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // TODO write custom selection behaviour
    }
}

并将其应用于列表视图:

<ListView bb:ListViewBehaviour.AutoUnselect="True">
    ...
</ListView>