2017-01-02 32 views
-1

ItemsControlの中にUserControlsがあります。ItemsControlからアイテムを削除

UserControl(私の場合はJobViewと呼ばれます)には、ContextMenuと1 Item( '削除')があります。今

UserControls ContextMenu上の項目は、私はItemsControlItemCollectionからそれを削除したいClickedです。

私はItemを取得する必要があり、ContextMenuはに割り当てられています。

現在、私はこれを使用しています:

private void Item_Click(object sender, RoutedEventArgs e) 
{ 

    MenuItem item = (MenuItem)sender; 
    JobView view = null; 

    FrameworkElement currentObject = item; 
    while(1 == 1) 
    { 
     currentObject = currentObject.Parent as FrameworkElement; 
     if(currentObject.GetType() == typeof(System.Windows.Controls.Primitives.Popup)) 
     { 
      view = (currentObject as System.Windows.Controls.Primitives.Popup).PlacementTarget as JobView; 
      break; 
     } 
    } 
    //Remove from ObservableCollection<JobView>: 
    JobViews.Remove(view); 
} 

をそれが正常に動作しているが、私は、私はいくつかのよりよい解決策がなければならないことをかなり確信しています。

私はそのことを理解するのに時間をかけましたが、私は自分自身で別の解決法に到達することはできません。

にはどうすればJobViewsenderオブジェクト を使用するか、この場合は完全に間違っsenderを使用している得ることができますか?

答えて

1

ItemsControlのItemsSourceをUIElementsまたはビューのObservableCollectionにバインドまたは設定するのは間違っています。少なくとも、すべてのXAMLベースのアプリケーションに使用する推奨パターンであるMVVMデザインパターンが気になる場合。

あなたのジョブ・ビューの状態を表すクラスを作成し、そのようなオブジェクトのObservableCollectionにバインドするの、例えば必要があります。

public class Job 
{ 
} 

public class JobViewModel 
{ 
    public ObservableCollection<Job> Jobs { get; } = new ObservableCollection<Job>() 
    { 
     new Job(), 
     new Job(), 
     new Job() 
    }; 
} 

あなたは、その後のItemsControlのItemTemplateににユーザーコントロール(ジョブ・ビュー)を使用します。

あなたは、ソースのコレクションからジョブ・クラスを削除するビューモデルのコマンドプロパティにバインドするジョブ・ビュークラスにICommandのプロパティを追加することができる場所ではこれにより
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new JobViewModel(); 
    } 
} 

<ItemsControl ItemsSource="{Binding Jobs}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <local:JobView /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

。次のサンプルコードを参照してください。

JobViewModel.cs:

public class JobViewModel 
{ 
    public JobViewModel() 
    { 
     RemoveCommand = new DelegateCommand<object>(argument => 
     { 
      Jobs.Remove(argument as Job); 
     }); 
    } 

    public ObservableCollection<Job> Jobs { get; } = new ObservableCollection<Job>() 
    { 
     new Job(), 
     new Job(), 
     new Job() 
    }; 

    public DelegateCommand<object> RemoveCommand { get; } 
} 

JobView.xaml.cs:

public partial class JobView : UserControl 
{ 
    public JobView() 
    { 
     InitializeComponent(); 
    } 

    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(JobView)); 

    public ICommand Command 
    { 
     get { return (ICommand)GetValue(CommandProperty); } 
     set { SetValue(CommandProperty, value); } 
    } 
} 

JobView.xaml:

<UserControl x:Class="WpfApplication1.JobView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfApplication1" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" 
      x:Name="uc"> 
    <UserControl.ContextMenu> 
     <ContextMenu> 
      <MenuItem Header="Remove" Command="{Binding PlacementTarget.Command, RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
         CommandParameter="{Binding}"/> 
     </ContextMenu> 
    </UserControl.ContextMenu> 
    <Grid> 
     <TextBlock>job view...</TextBlock> 
    </Grid> 
</UserControl> 

MainWindow.xaml:

<ItemsControl ItemsSource="{Binding Jobs}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <local:JobView Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

あなたはDelegateCommandクラスを自分で実装する必要がありますか、プリズムMVVMライブラリで利用できるものを使用することができますhttps://www.nuget.org/packages/Prism.Wpf/https://github.com/PrismLibrary/Prism/blob/master/Source/Prism/Commands/DelegateCommand.cs

PrismはNuGetを使用してインストールすることができます。

MVVMパターンの詳細については、https://msdn.microsoft.com/en-us/library/hh848246.aspxを参照してください。 XAMLアプリケーションを開発しているなら、それを学ぶことをお勧めします。

関連する問題