3

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

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

非常感谢!

xml:

<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>
4

2 回答 2

1

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

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>
于 2011-01-07T22:04:20.773 回答
0

您可以将 Tab 的 Visibility 属性绑定到您的应用程序类或您拥有的任何东西,以便仅在用户按下 New 时显示选项卡。

于 2011-01-07T21:32:27.260 回答