【问题标题】:Creating tabs in WinRT在 WinRT 中创建选项卡
【发布时间】:2012-05-31 00:21:45
【问题描述】:

我正在开发适用于 Windows 8 的 C#/XAML Metro 风格应用程序。WinRT 中的 XAML 没有“选项卡”控件。但是,我正在尝试模拟 Windows 8 商店中结果的外观。例如,此图像显示“概述”、“详细信息”和“评论”选项卡:

我如何创建这些?

单选按钮似乎很有意义。我想我可以使用 GroupName 来确保只选择一项。但是,如果我使用 RadioButton,我不知道如何使所选项目看起来是深灰色,而将其他选项设置为浅灰色。有人可以给我看一个 RadioButton 的 XAML,它没有显示小检查的东西吗?选中时为深灰色,未选中时为浅灰色。

非常感谢!

【问题讨论】:

    标签: c# xaml windows-8


    【解决方案1】:

    以下是单选按钮的样式,以使它们看起来/像标签一样工作:

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

    【讨论】:

    • 我将您的视觉状态调整为与FlipView 一起使用的ListView,如here 所述。
    • 这在 Windows 商店 XAML 上不起作用...是否有原因/修复?
    【解决方案2】:

    设置 ListBox 的样式优于设置单选按钮组的样式。

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

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

    <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; }
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        主要思想是覆盖默认的FlipViewControlTemplate:我添加了一个ListBox,它代表选项卡标题并删除了“下一个”和“上一个”FlipView 按钮。

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

        【讨论】:

        • 能否分享一下代码。我正在尝试实现这一点,但找不到方法。
        • @VedankKulshrestha,恐怕我已经没有这个版本的代码了,但我确实有一个旧的存储库,在那里我尝试实现一个可重用的选项卡控件。你可以在这里找到它:bitbucket.org/takemyoxygen/winrt-tabcontrol
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多