0

Observable Collection にバインドされたコンボボックスがあります。Collection は、自己定義クラスのコンテナーです。

ドロップダウンリストのアイテムの上にマウスカーソルを置いたときにマウスの右ボタンを押して、コンボボックスから任意のアイテムを削除する必要があります。また、アイテムが強調表示されているときに削除ボタンを押して削除する必要があります。

コード ビハインドには既に解決策がありますが、MVVM-Pattern を使用して解決する必要があります。

この問題で誰か助けてもらえますか?

事前に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>

人物クラス:

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;
                }
            }
        }
    }
4

2 に答える 2

0

問題を解決するために、これらのアプローチのいずれかを使用しました。

  1. MouseButtonEventArgs e.Source を Combobox にキャストし、上記のソリューションを適用してタスクを実行します。選択したタイプの 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. このコンボボックスのカスタム実装を使用して、itemindex をコード ビハインドのプロパティにバインドします。次に、MyCollection.RemoveAt(); を介してアイテムを削除できます。

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

于 2013-07-16T06:58:05.423 に答える