2011-04-21 62 views
2

我可以選擇在ListView多個項目。但是,如果我點擊一個,它變成藍色。這很正常,所以顯示它被選中。但是,如果我在同一項目再次點擊,它不取消。所以我不能改變我的選擇。有人誰知道如何解決這個愚蠢的小問題?選擇的項目沒有取消選擇

編輯:這是我的列表視圖:

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

我可以在代碼隱藏工作,具有OnSelectionChanged,如果選擇=真...但是這看起來有些奇怪,我..我希望有一個更好的辦法,只是一個屬性或某事 – Ruben 2011-04-21 08:29:02

回答

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

我正面臨着類似的問題,結果發現,雖然左擊總是選擇項目指出,您可以使用Ctrl +左鍵單擊以在列表視圖中切換選擇。這是默認行爲。

相關問題