在WinRT中创建选项卡

时间:2012-05-24 12:01:41

标签: c# xaml windows-8

我正在为Windows 8开发一个C#/ XAML Metro风格的应用程序.WinRT中的XAML没有“tab”控件。但是,我试图模仿Windows 8商店中的结果。例如,此图像显示“概述”,“详细信息”和“评论”标签:

enter image description here

如何创建这些?

RadioButton似乎有道理。我想我可以使用GroupName来确保只选择一个项目。但是,如果我使用RadioButton,我不知道如何使所选项目看起来是深灰色,而其他选项是浅灰色。有人能告诉我一个RadioButton的XAML,它没有显示那个小小的东西吗?选择时也是深灰色,未选中时是浅灰色。

非常感谢你!

4 个答案:

答案 0 :(得分:18)

这是用于单选按钮的样式,使它们看起来像标签一样工作:

    <!-- Style for radio buttons used as tab control -->
<Style x:Key="TabRadioButtonStyle" TargetType="RadioButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RadioButton">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Unchecked">
                                <Storyboard>
                                    <ColorAnimation Duration="0" To="Gray" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="TabButtonText" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Indeterminate">
                                <Storyboard>
                                    <ColorAnimation Duration="0" To="White" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="TabButtonText" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Checked">
                                <Storyboard>
                                    <ColorAnimation Duration="0" To="Black" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="TabButtonText" />
                                </Storyboard>
                            </VisualState>

                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <TextBlock x:Name="TabButtonText" Text="{TemplateBinding Content}" Style="{StaticResource GroupHeaderTextStyle}" HorizontalAlignment="Left"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后,您可以定义一个网格来保存选项卡堆栈面板和一个框架,以保存与每个选项卡关联的内容。使用单选按钮上的Click事件将框架导航到每个“选项卡”的相应页面。

 <Grid Grid.Row="1"
        Margin="120,0,56,56">
        <!-- Row 1 to hold the "Tabs", Row 2 to hold the content -->
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <RadioButton x:Name="Tab1" Content="Tab1" Style="{StaticResource TabRadioButtonStyle}" IsChecked="True" Click="Tab1_Clicked" />
            <RadioButton x:Name="Tab2" Content="Tab2" Style="{StaticResource TabRadioButtonStyle}" IsChecked="False" Click="Tab2_Clicked" Margin="30,0,0,0" />
            <RadioButton x:Name="Tab3" Content="Tab3" Style="{StaticResource TabRadioButtonStyle}" IsChecked="False" Click="Tab3_Clicked" Margin="30,0,0,0"/>
        </StackPanel>
        <Frame x:Name="ContentFrame" Margin="0,20,0,0" Grid.Row="1" Background="{StaticResource SandstormBackgroundBrush}" Loaded="ContentFrame_Loaded" />
    </Grid>

答案 1 :(得分:6)

设置ListBox样式比设置单选按钮组更可取。

以下代码使用带有水平堆栈面板的ListBox来创建标签项标题。 ContentControl将选项卡内容显示为用户控件。

我只用WPF对此进行了测试,但希望它能在WinRT上运行。

enter image description here

<Page.Resources>
    <Style TargetType="ListBoxItem">
        <!-- disable default selection highlight -->
        <!-- Style.Resources is not supported in WinRT -->
        <!--<Style.Resources>
            --><!-- SelectedItem with focus --><!--
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                            Color="Transparent" />
            --><!-- SelectedItem without focus --><!--
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                            Color="Transparent" />
        </Style.Resources>-->
        <!--Setter Property="FocusVisualStyle" is not supported in WinRT -->
        <!--<Setter Property="FocusVisualStyle" Value="{x:Null}" />-->
    </Style>
    <Style x:Key="TitleStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="LightGray"/>
        <!--Style.Triggers is not supported in WinRT-->
        <!--<Style.Triggers>
            <DataTrigger Value="True" Binding="{Binding Path=IsSelected, 
                        RelativeSource={RelativeSource Mode=FindAncestor, 
                        AncestorType={x:Type ListBoxItem}}}">
                <Setter Property="Foreground" Value="DarkGray"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
        </Style.Triggers>-->
    </Style>
</Page.Resources>
<Grid>
    <Grid.DataContext>
        <ViewModel:TestPage/>
    </Grid.DataContext>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <ListBox x:Name="tabListBox" Grid.Row="0" ItemsSource="{Binding Items}" 
                        SelectedItem="{Binding SelectedItem, Mode=TwoWay}" 
                        BorderBrush="{x:Null}" BorderThickness="0">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}" Margin="5" 
                        Style="{StaticResource TitleStyle}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <ContentControl Grid.Row="1" Content="{Binding SelectedItem.Content}"/>
</Grid>

查看模型

public class MyTabViewModel : INotifyPropertyChanged
{
    public MyTabViewModel()
    {
        Items =
            new List<MyTabItem>
                {
                    new MyTabItem
                        {
                            Title = "Overview",
                            Content = new UserControl1()
                        },
                    new MyTabItem
                        {
                            Title = "Detail",
                            Content = new UserControl2()
                        },
                    new MyTabItem
                        {
                            Title = "Reviews",
                            Content = new UserControl3()
                        },
                };
    }

    public IEnumerable<MyTabItem> Items { get; private set; }

    private MyTabItem _selectedItem;

    public MyTabItem SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
        }
    }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

public class MyTabItem
{
    public string Title { get; set; }
    public UserControl Content { get; set; }
}

答案 2 :(得分:5)

FlipView control可能会满足您的需求。 Sample

答案 3 :(得分:0)

我也使用了FlipView控件,但是我创建了一个单独的模板化控件,它继承自FlipView

主要想法是覆盖默认FlipView ControlTemplate:我添加了一个代表标签标题的ListBox,并删除了“下一个”和“上一个”FlipView按钮。

如果您需要有关我的实施的更多详细信息,我可以共享源代码。