【问题标题】:Selected item in a listview doesn't unselect列表视图中的选定项目不会取消选择
【发布时间】:2011-04-21 08:13:44
【问题描述】:

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

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

 <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>

【问题讨论】:

  • 我可以在代码隐藏中工作,使用 OnSelectionChanged,如果 selected=true... 但这对我来说有些奇怪.. 我希望有更好的方法,只是一个属性或其他东西

标签: wpf listview mvvm selection


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以编写一个 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>
    

    【讨论】:

      猜你喜欢
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      相关资源
      最近更新 更多