Wpf 列表视图中的选定项不存在';不要取消选择
我可以在列表视图中选择多个项目。但如果我点击一个,它会变成蓝色。这是正常的,因此显示它已被选中。但是如果我再次点击同一个项目,它不会取消选中。所以我不能改变我的选择。知道如何解决这个愚蠢的小问题的人 编辑:这是我的列表视图:Wpf 列表视图中的选定项不存在';不要取消选择,wpf,listview,mvvm,selection,Wpf,Listview,Mvvm,Selection,我可以在列表视图中选择多个项目。但如果我点击一个,它会变成蓝色。这是正常的,因此显示它已被选中。但是如果我再次点击同一个项目,它不会取消选中。所以我不能改变我的选择。知道如何解决这个愚蠢的小问题的人 编辑:这是我的列表视图: <ListView Height="155" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectedItem="{Bindin
<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>
您可以编写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
}
}
公共类ListViewBehavior
{
///
///Enfoca自动项目选择
///
公共静态只读从属属性AutoUnselectProperty=
DependencyProperty.RegisterAttached(
“自动取消选择”,
类型(bool),
类型(ListViewBehavior),
新的UIPropertyMetadata(false,OnAutoUnselectItemChanged);
公共静态bool GetAutoUnselectItem(ListView listBox)
{
return(bool)listBox.GetValue(autounselecttemproperty);
}
公共静态void SetAutoUnselectItem(ListView列表框,布尔值)
{
SetValue(自动取消选择属性,值);
}
AutoUnselectItemChanged上的私有静态无效(DependencyObject源,DependencyPropertyChangedEventArgs e)
{
var listView=源作为listView;
如果(listView==null)
返回;
如果(e.NewValue为bool==false)
listView.SelectionChanged-=OnSelectionChanged;
其他的
listView.SelectionChanged+=OnSelectionChanged;
}
SelectionChanged上的私有静态无效(对象发送方,SelectionChangedEventArgs e)
{
//TODO写入自定义选择行为
}
}
并将其应用于列表视图:
<ListView bb:ListViewBehaviour.AutoUnselect="True">
...
</ListView>
...
我遇到了一个类似的问题,并发现,尽管左键单击总是选择指向的项目,但您可以使用Ctrl+左键单击在列表视图中切换选择。这是默认行为。我可以在codebehind中工作,OnSelectionChanged,如果selected=true。。。但我觉得这有点奇怪。。我希望有一个更好的方式,只是一个财产或东西