English 中文(简体)
在 WinRT 中创建标签
原标题:Creating tabs in WinRT

我正在为 Windows 8 开发 C#/ XAML Metro 风格应用程序。 WinRT 中的 XAML 没有“ tab” 控制。 然而, 我试图模仿 Windows 8 商店的外观结果。 例如, 这张照片显示“ 概览”、“ 细节” 和“ 审查” 标签 :

""https://i.sstatic.net/eI2K5.jpg" alt="在这里输入图像描述"/ >

我如何创建这些?

放射性按钮似乎有道理 。 我想可以使用组名来确保只选中一个项目 。 但如果我使用一个放射性按钮, 我不知道如何让选中的项目看起来黑暗灰色, 同时又让其他选项变浅灰色 。 有人能给我看一个不显示小检查物件的无线电按钮 XAML 吗? 如果选中了, 也可以是暗灰色 ; 如果未选中, 则是浅灰色 。

太谢谢你了!

问题回答

以下是用于收听按钮的样式, 使其看起来/ 工作像标签 :

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

然后您可以定义一个屏蔽制表板的网格和一个屏蔽每个制表板相关内容的框。在无线电按钮上单击事件将框架浏览到每个“ tab”的适当页面。

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

给列表框打字比给收音机按钮组打字更好。

以下代码使用带有水平堆叠面板的 ListBox 创建制表符项标题。一个内容控制器将制表符内容显示为用户控制 。

我只用WPF测试过这个 但希望能用WinRT

""https://i.sstatic.net/6anR9.png" alt="此处的内置图像描述"/"

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

FlipView control 可能满足你的需要。Sample

我也使用了 FlipView 控制,但我创建了一个单独的模板控件,由 FlipView 继承。

主要的想法是推翻默认的 FlipView controlTemplate :我添加了一个 ListBox ,它代表了制表板头,并删除了“下一个”和“前” FlipView 按钮。

我可以分享源代码,如果你需要 更多关于我执行的细节。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签