我有一个带有工具栏和 TabContent 区域(PRISM)的视图(主视图)。在 TabContent 区域中,我有两个选项卡(两个视图 - 视图 A,视图 B-)以不同的方式表示相同的模型(联系人)。
视图 A 包含一个带有联系人的 DataGrid。主视图中的工具栏包含一个带有 DeleteCommand 的“删除按钮”。我想使用 DeleteCommand 将视图 A 中选定的联系人作为命令参数发送,但是使用下面显示的代码,命令参数为空。看起来主视图没有从位于视图 A 中的 DataGrid 中检索选定的项目。我该如何做到这一点?
这是主视图:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" Background="#D6D6DC">
<ToolBar Style="{StaticResource ModuleToolBarStyle}">
<TextBlock Margin="10,0,0,0" Text="Contacts"></TextBlock>
<Button Name="addContactButton" ToolTip="Add Contact">
<Image Source="/PrismApp.Controls;component/Images/add.png"/>
</Button>
<Button Name="deleteContactsButton" ToolTip="Delete selected Contacts"
Command="{Binding DeleteContactCommand}" CommandParameter="{Binding SelectedItems, ElementName=ContactsList}">
<Image Source="/PrismApp.Controls;component/Images/delete.png"/>
</Button>
<ToggleButton Name="ViewAButton" ToolTip="View A" Command="{Binding NavigateToViewACommand}"
IsChecked="{Binding IsViewAActive}">
<Image Source="/PrismApp.Controls;component/Images/listblack.png"/>
</ToggleButton>
<ToggleButton Name="ViewBButton" ToolTip="View B" Command="{Binding NavigateToViewBCommand}"
IsChecked="{Binding IsViewBActive}">
<Image Source="/PrismApp.Controls;component/Images/tilesblack.png"/>
</ToggleButton>
</ToolBar>
</DockPanel>
<TabControl Grid.Row="1" prism:RegionManager.RegionName="ContactsViewRegion">
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Grid>
这是视图 A:
<Grid>
<DataGrid x:Name="ContactsList" Margin="20" AutoGenerateColumns="False" IsReadOnly="True" CanUserResizeRows="False"
CanUserResizeColumns="True" ColumnWidth="*" ItemsSource="{Binding Contacts}">
</DataGrid>
</Grid>
主视图、视图 A 和视图 B 的视图模型相同。