我有一個組合框綁定到可觀察集合。 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;
}
}
}
}
我不認爲需要在這個'((ComboBoxItem)CBI)投.IsHighlighted',只是'cbi.IsHighlighted'是OK。 –
是的你是對的,謝謝你的提示:)。 –