gpt4 book ai didi

c# - 如何删除 Caliburn Micro MVVM 应用程序中的关闭选项卡?

转载 作者:行者123 更新时间:2023-12-03 10:43:01 28 4
gpt4 key购买 nike

我已经使用 Caliburn Micro 在 MVVM 中构建了一个应用程序。我目前在我的所有选项卡上都有 closetab,它会关闭除前三个选项卡之外的所有选项卡,即主页、付款、注释,但是,我希望它不在前三个选项卡上显示关闭选项卡图标,如图所示我的 AppViewModel 代码:

<TabControl x:Name="Items" Grid.Row="1" Visibility="{Binding Visibility, Converter={StaticResource boolToVis}}">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- The Tab Names Binding with DisplayName-->
<TextBlock Text="{Binding DisplayName}" />
<!-- The Tab Close Icon-->
<Button Content="x" x:Name="CloseTab" cal:Message.Attach="CloseTab" Style="{DynamicResource appTabCloseButton}" Visibility="{Binding Visibility, Converter={StaticResource boolToVis}}"/>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>

关闭选项卡后面的我的 AppViewModel 代码:
public void CloseTab()
{
if(ActiveItem.DisplayName == "Home" || ActiveItem.DisplayName == "Payment" || ActiveItem.DisplayName == "Notes")
{
MessageBox.Show("This Tab Cannot Be closed.","Permanent Tab");

} else {
DeactivateItem(ActiveItem, close: true);
}
}

我的 boolToVis 的 App.xaml 代码:
<Application x:Class="WPF.Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF.Test.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:Bootstrapper x:Key="bootstrapper" />
<BooleanToVisibilityConverter x:Key="boolToVis" />
</ResourceDictionary>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Global.WPF.UserControls;component/Resources/brushes.xaml" />
<ResourceDictionary Source="/Global.WPF.UserControls;component/Resources/CommonControls.xaml" />
<ResourceDictionary Source="/Global.WPF.UserControls;component/Resources/menuItems.xaml" />
<ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

最后是绑定(bind)的可见性:
bool _visibility;
public bool Visibility
{
get { return _visibility; }
set
{
_visibility = value;
NotifyOfPropertyChange("Visibility");
}
}

我很高兴有任何建议,因为我完全迷路了!

最佳答案

创建一个可用于显示/隐藏按钮的保护面板。

<TabControl x:Name="Items" Grid.Row="1" Visibility="{Binding Visibility, Converter={StaticResource boolToVis}}">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- The Tab Names Binding with DisplayName-->
<TextBlock Text="{Binding DisplayName}" />
<Border x:Name="CanCloseTab">
<!-- The Tab Close Icon-->
<Button Content="x" x:Name="CloseTab" cal:Message.Attach="CloseTab" Style="{DynamicResource appTabCloseButton}" />
</Border>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>

然后创建一个守卫属性
public bool CanCloseTab {
get {
return !(ActiveItem.DisplayName == "Home"
|| ActiveItem.DisplayName == "Payment"
|| ActiveItem.DisplayName == "Notes");
}
}

按照惯例, View 应该自动绑定(bind) CanCloseTab属性为面板(边框)的可见性,以便当它为 false 时不会显示。如果面板不存在,则保护属性也会自动禁用该按钮。

为了简单起见,这现在应该允许重构 CloseTab 方法。
public void CloseTab() {
if(CanCloseTab) {
DeactivateItem(ActiveItem, close: true);
}
}

关于c# - 如何删除 Caliburn Micro MVVM 应用程序中的关闭选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45866818/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com