WPF C# 在应用程序启动时隐藏 TabControl 项

发布于 2024-10-10 18:44:29 字数 2704 浏览 3 评论 0原文

我创建了一个在应用程序运行时加载的起始页,但它也显示了我的选项卡控件(两个用于编辑和图表的选项卡项)。

如何在启动时隐藏我的选项卡控件项目并仅在用户选择文件 -> 时才显示它新的?

非常感谢!

Xaml:

<TabControl Height="Auto" Name="tabControl1" Width="Auto">
            <TabItem Header="Diagram" Name="DiagramTab"></TabItem>
            <TabItem Header="Rulebase" Name="RuleTab" >
              <Grid>
               <TextBox Height="Auto" Name="RuleText" Width="Auto" Text="" AcceptsTab="True" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" GotFocus="FocusChanged" KeyDown="ContentChanged" HorizontalScrollBarVisibility="Visible" />
              </Grid>
           </TabItem>
        </TabControl>

这是我的文件 ->新菜单项:

 private void NewItem(object sender, RoutedEventArgs e)
    {
        ProcessNewCommand();
    }

private void ProcessNewCommand()
    {
        if (dataChanged)
        {
            string sf = SaveFirst();
            if (sf != "Cancel")
            {
                ClearState();                    
            }
        }
        else
        {
            ClearState();
        }
    }

风格:

<Style TargetType="TabItem" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabItem">
                    <Grid>
                        <Border 
                            Name="Border"
                            Background="LightBlue"
                            BorderBrush="Black" 
                            BorderThickness="0" 
                            CornerRadius="6,6,0,0" >
                 <ContentPresenter x:Name="ContentSite"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            ContentSource="Header"
                            Margin="12,2,12,2"/>
                        </Border>
                   </Grid>
         <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="LightBlue" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="LightGray" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I've created a start page that loads when the application is run but it is also showing my tabcontrol (two tabitems for editing & diagraming).

How do I hide my tabcontrol items on startup and only show it when a user selects file -> new?

Many thanks!

Xaml:

<TabControl Height="Auto" Name="tabControl1" Width="Auto">
            <TabItem Header="Diagram" Name="DiagramTab"></TabItem>
            <TabItem Header="Rulebase" Name="RuleTab" >
              <Grid>
               <TextBox Height="Auto" Name="RuleText" Width="Auto" Text="" AcceptsTab="True" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" GotFocus="FocusChanged" KeyDown="ContentChanged" HorizontalScrollBarVisibility="Visible" />
              </Grid>
           </TabItem>
        </TabControl>

Here's my file -> new menu item:

 private void NewItem(object sender, RoutedEventArgs e)
    {
        ProcessNewCommand();
    }

private void ProcessNewCommand()
    {
        if (dataChanged)
        {
            string sf = SaveFirst();
            if (sf != "Cancel")
            {
                ClearState();                    
            }
        }
        else
        {
            ClearState();
        }
    }

Style:

<Style TargetType="TabItem" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabItem">
                    <Grid>
                        <Border 
                            Name="Border"
                            Background="LightBlue"
                            BorderBrush="Black" 
                            BorderThickness="0" 
                            CornerRadius="6,6,0,0" >
                 <ContentPresenter x:Name="ContentSite"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            ContentSource="Header"
                            Margin="12,2,12,2"/>
                        </Border>
                   </Grid>
         <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="LightBlue" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="LightGray" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑叹一世浮沉 2024-10-17 18:44:29

在这些场景中,我通常有一个 ProjectViewModel (或 ProjectModel),它被添加为包含 WindowDataContextTabControl 可以将这些选项卡绑定到数据上下文上的项目。

public class ProjectViewModel : YourViewModelBase
{
    public EditingViewModel Editor { ... }

    public DiagramingViewModel Diagram { ... }
}

因此,当 NewCommand 触发时,您会说 this.DataContext = new ProjectViewModel()。下面的样式将处理其余的事情。

<Window.Resources>
    <Style x:Key="HideWithoutDataContext">
        <Setter Property="UIElement.Visibility" Value="Visible" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding}" Value="{x:Null}">
                <Setter Property="UIElement.Visibility" Value="Collapsed" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<TabControl>
    <TabItem Header="Start Page">
       ...
    </TabItem>
    <TabItem Header="Editor"
             DataContext="{Binding Editor}"
             Style="{DynamicResource HideWithoutDataContext}"/>
    <TabItem Header="Diagram"
             DataContext="{Binding Diagram}"
             Style="{DynamicResource HideWithoutDataContext}"/>
</TabControl>

In these scenarios I usually have a ProjectViewModel (or ProjectModel) that gets added as the DataContext of the containing Window. The TabControl could have those tabs bound to the items on the data context.

public class ProjectViewModel : YourViewModelBase
{
    public EditingViewModel Editor { ... }

    public DiagramingViewModel Diagram { ... }
}

So when the NewCommand fires you would say this.DataContext = new ProjectViewModel(). The style below will take care of the rest.

<Window.Resources>
    <Style x:Key="HideWithoutDataContext">
        <Setter Property="UIElement.Visibility" Value="Visible" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding}" Value="{x:Null}">
                <Setter Property="UIElement.Visibility" Value="Collapsed" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<TabControl>
    <TabItem Header="Start Page">
       ...
    </TabItem>
    <TabItem Header="Editor"
             DataContext="{Binding Editor}"
             Style="{DynamicResource HideWithoutDataContext}"/>
    <TabItem Header="Diagram"
             DataContext="{Binding Diagram}"
             Style="{DynamicResource HideWithoutDataContext}"/>
</TabControl>
十秒萌定你 2024-10-17 18:44:29

您可以将选项卡的可见性属性绑定到您的应用程序类或任何您拥有的类,这样只有在用户按下“新建”时才显示选项卡。

You could bind the Tab's Visibility property to your application class or whatever you have so you only show the tabs once the user presses New.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文