我在UserControls
内有一些ItemsControl
。
每个UserControl
(在我的情况下称为JobView
)都有一个ContextMenu
,其中包含Item
('删除')。
现在当UserControls ContextMenu
上的项目为Clicked
时,我想将其从ItemsControlItemCollection
中删除。
为此,我需要获取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);
}
工作正常,但我很确定我必须有更好的解决方案。
我花了一些时间来解决这个问题,但我自己无法找到不同的解决方案。
如何使用JobView
对象获取sender
或者在这种情况下使用sender
完全错误?
答案 0 :(得分:2)
将ItemsControl的ItemsSource绑定或设置为UIElements或视图的ObservableCollection是错误的。至少如果你关心MVVM设计模式,它是推荐用于所有基于XAML的应用程序的模式。
您应该创建一个表示JobView状态的类,并绑定到此类对象的ObservableCollection,例如:
public class Job
{
}
public class JobViewModel
{
public ObservableCollection<Job> Jobs { get; } = new ObservableCollection<Job>()
{
new Job(),
new Job(),
new Job()
};
}
然后在ItemsControl的ItemTemplate中使用UserControl(JobView):
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new JobViewModel();
}
}
<ItemsControl ItemsSource="{Binding Jobs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:JobView />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
有了这个,您就可以向JobView类添加一个ICommand属性,该属性绑定到视图模型的命令属性,该属性从源集合中删除Job类。请参阅以下示例代码。
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类,或者可以使用Prism MVVM库中提供的类:https://github.com/PrismLibrary/Prism/blob/master/Source/Prism/Commands/DelegateCommand.cs
可以使用NuGet安装Prism:https://www.nuget.org/packages/Prism.Wpf/。
您可以在此处阅读有关MVVM模式的更多信息:https://msdn.microsoft.com/en-us/library/hh848246.aspx。如果您正在开发XAML应用程序,我真的建议您学习它。