在WPF(C#)应用程序中,我的目标是从子视图中执行删除命令。 Delete命令将从上级集合中删除对象本身。 这是孩子的数据模板(来自.xaml文件)
<DataTemplate DataType="{x:Type dataModel:AppLeg}">
<Menu Grid.Column="2">
<MenuItem Header="Delete Leg" Command="{Binding DeleteLegCommand}"
CommandParameter="{Binding}"/>
</Menu>
</DataTemplate>
<DataTemplate DataType="{x:Type dataModel:DestinationSchedule}">
<ItemsControl dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding}"
Focusable="False"
ItemsSource="{Binding AppLegs}"
SnapsToDevicePixels="True"
FontWeight="SemiBold"
UseLayoutRounding="True">
</ItemsControl>
</DataTemplate>
在DataModel中,正在执行Command_DeleteTripLeg方法,这意味着还调用了Event EventDeleteLeg。
public class AppLeg
{
public event Action<AppLeg> EventDeleteLeg;
public RelayCommand<object> DeleteTripLegCommand => new
RelayCommand<object>(Command_DeleteLeg, true);
private void Command_DeleteTripLeg(object leg )
{
EventDeleteLeg?.Invoke((AppLeg)Leg);
}
现在,在父类(AppLeg类的Collection类)中,我有Appleg类的collection对象。
public class DestinationSchedule : ViewModelBase
{
#region Fields
private ObservableCollection<AppLeg> _appLegs;
private void deleteAppLegFromCollection( AppLeg appLeg)
{
_appLegs.Remove( appLeg )
}
}
所以我的目标是将父DestinationSchedule模型类的deleteAppLegFromCollection()方法订阅到EventDeleteLeg事件,以便在执行“ DeleteLegCommand”命令时,可以从集合_tripLegs中删除该AppLeg对象。
AppDestination.cs类
public class AppDestination : ViewModelBase
{
private ObservableCollection<DestinationSchedule> _destinationSchedule;
public ObservableCollection<DestinationSchedule> DestinationSchedule
{
get => _destinationSchedule;
set => Set(ref _destinationSchedule, value);
}
}
ScheduleDestinationsView.xaml代码
<d:UserControl.DataContext>
<viewModel:ScheduleDestinationsViewModel/>
</d:UserControl.DataContext>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/SharedResources;
component/DataTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid SnapsToDevicePixels="True" UseLayoutRounding="True">
<ListBox VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch"
Focusable="False"
ItemsSource="{Binding AppDestinations}">
<ItemsControl.Template>
<ControlTemplate>
<ItemsPresenter/>
</ControlTemplate>
</ItemsControl.Template>
</ListBox>
</Grid>
和ScheduleDestinationsViewModel.cs类
public class ScheduleDestinationsViewModel : ViewModelBase
{
private ObservableCollection<AppDestination> _appDestinations;
public ObservableCollection<AppDestination> AppDestinations
{
get => _appDestinations;
set => Set(ref _appDestinations, value);
}
public ScheduleDestinationsViewModel()
{
AppDestinations = new ObservableCollection<AppDestination>();
}
}
父类如何订阅子类活动? 请指教。另一种方法也很不错。 谢谢。
答案 0 :(得分:1)
将delete命令移动到定义了DestinationSchedule
源集合的父类(AppLegs
)上,并调用该命令的命令。
您可以通过指定RelativeSource
绑定到父元素的命令:
<Menu Grid.Column="2">
<MenuItem Header="Delete Leg" Command="{Binding DataContext.DeleteLegCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"/>
</Menu>