2013-07-11 17 views
0

我有一個組合框綁定到可觀察集合。 Collection是一個自定義類的容器。WPF從組合框中刪除項目時將鼠標懸停在組合框上(MVVM)

我需要通過在下拉列表中的項目我懸停鼠標時,按鼠標右鍵刪除組合框的arbitary項目。當項目突出顯示時,我還需要通過按刪除按鈕將其刪除。

我已經在後面的代碼中有一個解決方案,但我需要使用MVVM模式。

任何人都可以幫助我在這個問題上?

Thx提前:)。

這裏是我的代碼:

我的視圖模型:

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Text; 
using Catel.MVVM; 
using System.Windows.Input; 
using DeleteItemFromComboBox.Models; 
using Catel.Data; 

namespace DeleteItemFromComboBox.ViewModels 
{ 
    public class MainWindowVM : ViewModelBase 
    { 

     #region Constructors 
     /// <summary> 
     /// Initializes a new instance of the <see cref="MainWindowVM"/> class. 
     /// </summary> 
     public MainWindowVM() 
     { 
      PreviewKeyDownCmd = new Command<KeyEventArgs>(PreviewKeyDownCmdExecute); 
      PersonList = new ObservableCollection<Person>(); 
      PersonList.Add(new Person("AA")); 
      PersonList.Add(new Person("BB")); 
     } 
     #endregion 


     #region Properties 
     /// <summary> 
     /// Gets or sets the property value. 
     /// </summary> 
     public ObservableCollection<Person> PersonList 
     { 
      get { return GetValue<ObservableCollection<Person>>(PersonListProperty); } 
      set { SetValue(PersonListProperty, value); } 
     } 

     /// <summary> 
     /// Register the PersonList property so it is known in the class. 
     /// </summary> 
     public static readonly PropertyData PersonListProperty = 
      RegisterProperty("PersonList", typeof(ObservableCollection<Person>), null); 
     #endregion 


     #region Commands 
     /// <summary> 
     /// Gets the PreviewKeyDownCmd command. 
     /// </summary> 
     public Command<KeyEventArgs> PreviewKeyDownCmd { get; private set; } 

     /// <summary> 
     /// Method to invoke when the PreviewKeyDownCmd command is executed. 
     /// </summary> 
     private void PreviewKeyDownCmdExecute(KeyEventArgs e) 
     { 
      if (e.Key == Key.Delete) 
      { 
       //********************What Should i do here?*************************** 
      } 
     } 
     #endregion 
    } 
} 

XAML文件:

<Window x:Class="DeleteItemFromComboBox.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ViewModels="clr-namespace:DeleteItemFromComboBox.ViewModels" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:catel="http://catel.codeplex.com"> 

<Window.Resources> 
    <ViewModels:MainWindowVM x:Key="ViewModel"/> 
</Window.Resources> 

<Grid DataContext="{Binding Source={StaticResource ViewModel}}"> 
    <ComboBox Height="44" 
       HorizontalAlignment="Left" 
       Margin="12,12,0,0" 
       Name="comboBox1" 
       VerticalAlignment="Top" 
       Width="479" 
       ItemsSource="{Binding PersonList, Mode=TwoWay}" > 

     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="PreviewKeyDown"> 
       <catel:EventToCommand Command="{Binding PreviewKeyDownCmd}" DisableAssociatedObjectOnCannotExecute="False" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 

    </ComboBox> 
</Grid> 

Person類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Catel.MVVM; 
using Catel.Data; 
using System.Runtime.Serialization; 

namespace DeleteItemFromComboBox.Models 
{ 

#if !SILVERLIGHT 
    [Serializable] 
#endif 
    public class Person : ModelBase 
    { 

     #region Constructors 

     public Person() { } 

     public Person(string name) 
     { 
      this.Name = name; 
     } 

#if !SILVERLIGHT 
     protected Person(SerializationInfo info, StreamingContext context) 
      : base(info, context) { } 
#endif 
     #endregion 

     /// <summary> 
     /// Gets or sets the property value. 
     /// </summary> 
     [Model] 
     public string Name 
     { 
      get { return GetValue<string>(NameProperty); } 
      private set { SetValue(NameProperty, value); } 
     } 

     /// <summary> 
     /// Register the Name property so it is known in the class. 
     /// </summary> 
     public static readonly PropertyData NameProperty = 
      RegisterProperty("Name", typeof(string)); 

     public override string ToString() 
     { 
      return Name; 
     } 
    } 
} 

解決方案在代碼隱藏在非MVVM項目:

private void comboBox1_PreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Delete) 
     { 
      foreach (People item in comboBox1.Items) 
      { 
       ComboBoxItem cbi = this.comboBox1.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem; 

       if (cbi.IsHighlighted == true) 
       { 
        peoples.Remove(item); 
        return; 
       } 
      } 
     } 
    } 
+0

我不認爲需要在這個'((ComboBoxItem)CBI)投.IsHighlighted',只是'cbi.IsHighlighted'是OK。 –

+0

是的你是對的,謝謝你的提示:)。 –

回答

0

我用這些方法之一來解決問題:

  1. 演員的MouseButtonEventArgs e.Source到組合框,然後申請我上面提到的做任務的解決方案。只需刪除您選擇的IPlan。

    private void Plan_PreviewMouseRightButtonDownCmd_Execute(MouseButtonEventArgs e) 
    { 
        ComboBox comboBox = e.Source as ComboBox; 
        if(comboBox!=null) 
        { 
         foreach (IPlan item in comboBox.Items) 
         { 
          ComboBoxItem cbi = comboBox.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem; 
    
          if (cbi.IsHighlighted == true) 
           SelectedPlans.Remove(item); 
    
          if (item == SelectedPlan) 
           SelectedPlan = null; 
         } 
        } 
    } 
    
  2. 使用此自定義的combobox實現並將itemindex綁定到代碼後面的屬性。然後你可以通過MyCollection.RemoveAt()刪除該項目;

http://www.codeproject.com/Articles/14255/ComboBox-firing-events-when-hovering-on-the-dropdo

0

最簡單的方法是創建一個屬性SelectedPerson。只要用戶右鍵單擊某個項目,它就會自動設置SelectedPerson。然後,您還可以創建一個工具欄,使用與彈出相同的命令刪除列表中的選定項目。

當您使用SelectedPerson方法,你可以使用這樣的代碼:

MyCollection.Remove(SelectedPerson); 
SelectedPerson = null; 

確保在您的OnCanExecute,你檢查是否SelectedPerson!= NULL。

+0

嗨吉爾特,謝謝你的快速回答。這可能是一個很好的替代解決方案但不幸的是,我需要實現上面,因爲selectedperson被綁定到selectedchanged事件和系統更新,只有當所選擇的項目被改變selectedperson的詢問功能。當項目選擇的項目被更改時,我的程序需要處理其他任務。順便說一句,你的CATEL框架非常成熟,支持很棒:)。我喜歡它。保持良好的工作 :)。 –

相關問題