使用 Prism - WPF 关闭动态添加的标签项 [英] Close dynamically added Tab Items using Prism - WPF
问题描述
我正在使用 Prism RegionManager
,通过 MainView
内的 TabControl
区域注册不同的视图.
I'm using Prism RegionManager
, to register different views with a TabControl
region inside the MainView
.
主视图.xaml
<TabControl regions:RegionManager.RegionName="MainViewTabRegion">
<TabControl.ItemTemplate>
<DataTemplate>
<DockPanel Width="Auto">
<Button Command="{Binding DataContext.DataContext.CloseTabCommand, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
Content="X"
Cursor="Hand"
DockPanel.Dock="Right"
Focusable="False"
FontFamily="Courier"
FontWeight="Bold"
Margin="4,0,0,0"
FontSize="10"
VerticalContentAlignment="Center"
Width="15" Height="15" />
<ContentPresenter Content="{Binding DataContext.DataContext.HeaderText, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" />
</DockPanel>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
在 MainViewViewModel 中,我添加了具有相同基类的不同视图.
In MainViewViewModel I'm Adding different views with the same base class.
MainViewViewModel.cs:
MainViewViewModel.cs:
private void AddProjectView() {
var view = _container.Resolve<ProjectSettingsView>();
var dataContext = _container.Resolve<ProjectSettingsViewModel>();
dataContext.HeaderText = "test header txt";
view.DataContext = dataContext;
_regionManager.RegisterViewWithRegion("MainViewTabRegion", () => view);
}
我可以在视图中添加新的标签项.
I can add new tab item with the view.
如何关闭标签项,上面XAML代码中的
,在ProjectSettingsViewModel中添加了一个带有
,与 TabItem 绑定.CloseCommand
的关闭按钮
How can I close the tab item, the <TabControl.ItemTemplate>
in the XAML code above, adds a close button with CloseCommand
in the ProjectSettingsViewModel
, with the TabItem bonded to it.
ProjectSettingsViewModel.cs
ProjectSettingsViewModel.cs
private void OnExecuteCloseCommand(object tabItem) {
//Close this TabItem
}
推荐答案
将 Button
的 CommandParameter
属性绑定到 DataContext
TabItem
:
Bind the CommandParameter
property of the Button
to the DataContext
of the TabItem
:
<Button Command="{Binding DataContext.DataContext.CloseTabCommand, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
Content="X"
Cursor="Hand"
DockPanel.Dock="Right"
Focusable="False"
FontFamily="Courier"
FontWeight="Bold"
Margin="4,0,0,0"
FontSize="10"
VerticalContentAlignment="Center"
Width="15" Height="15" />
然后您可以在视图模型中像这样删除视图:
You could then remove the view like this in the view model:
public class ProjectSettingsViewModel
{
private readonly IRegionManager _regionManager;
public ProjectSettingsViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
CloseTabCommand = new DelegateCommand<object>(OnExecuteCloseCommand);
}
private void OnExecuteCloseCommand(object tabItem)
{
_regionManager.Regions["MainViewTabRegion"].Remove(tabItem);
}
public DelegateCommand<object> CloseTabCommand { get; }
}
这篇关于使用 Prism - WPF 关闭动态添加的标签项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!