【问题标题】:Windows Phone XAML CheckBox AlignmentWindows Phone XAML 复选框对齐
【发布时间】:2014-12-14 06:51:04
【问题描述】:

我的 XAML 布局有问题,这在我的 Silverlight 应用程序上运行良好,但在使用 Windows 运行时,所有复选框从网格中完全消失,但文本块完美对齐。我不知道要更改我的 XAML 中的哪些内容才能使其正常工作。

我发现罪魁祸首是 Horizo​​ntalAlignment 属性,没有它,复选框会出现在网格中(尽管全部对齐到列的左侧)。文本块使用相同的属性,但它们看起来完全没有问题。

这是使用 Silverlight 时的样子:

<Grid Height="Auto" VerticalAlignment="Top" Margin="0,10,0,0">

    <Grid.ColumnDefinitions>

        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>

    </Grid.RowDefinitions>

    <CheckBox Name="ARStory" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0"/>
    <CheckBox Name="ARPath1" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="1"/>
    <CheckBox Name="ARPath2" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="2"/>
    <CheckBox Name="ARPath3" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="3"/>
    <CheckBox Name="ARPath4" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="4"/>
    <TextBlock Text="Story" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center"/>
    <TextBlock Text="Path 1" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center"/>
    <TextBlock Text="Path 2" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center"/>
    <TextBlock Text="Path 3" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center"/>
    <TextBlock Text="Path 4" Grid.Row="1" Grid.Column="4" HorizontalAlignment="Center"/>

</Grid>

【问题讨论】:

    标签: xaml windows-runtime winrt-xaml windows-phone-8.1


    【解决方案1】:

    我宁愿编辑复选框模板,而不是将框和标签分开,因为:

    • 用户希望可以点击复选框控件的内容/标签来选中/取消选中该框。
    • 如果点击标签,整个复选框控件将作为正常指针向下主题动画的一部分倾斜。
    • 我相信还有一些可访问性的原因(例如屏幕阅读器将能够正确解释控件)。
    • 您可以轻松地在任何地方重复使用该样式。
    <Page.Resources>
        <Style x:Key="CheckBoxStyleCentered" TargetType="CheckBox">
            <Setter Property="Background" Value="{ThemeResource CheckBoxBackgroundThemeBrush}"/>
            <Setter Property="BorderBrush" Value="{ThemeResource CheckBoxBorderThemeBrush}"/>
            <Setter Property="BorderThickness" Value="{ThemeResource PhoneBorderThickness}"/>
            <Setter Property="FontSize" Value="{ThemeResource TextStyleLargeFontSize}"/>
            <Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Top"/>
            <Setter Property="Padding" Value="0,10,0,0"/>
            <Setter Property="MinWidth" Value="50"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="CheckBox">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition From="Pressed" To="PointerOver">
                                            <Storyboard>
                                                <PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
                                            </Storyboard>
                                        </VisualTransition>
                                        <VisualTransition From="PointerOver" To="Normal">
                                            <Storyboard>
                                                <PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
                                            </Storyboard>
                                        </VisualTransition>
                                        <VisualTransition From="Pressed" To="Normal">
                                            <Storyboard>
                                                <PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
                                            </Storyboard>
                                        </VisualTransition>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="PointerOver"/>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <PointerDownThemeAnimation Storyboard.TargetName="Grid"/>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="CheckBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxPressedBackgroundThemeBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="CheckGlyph">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxPressedForegroundThemeBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="NormalRectangle">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxPressedBackgroundThemeBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="CheckBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxDisabledBorderThemeBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="CheckGlyph">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxDisabledForegroundThemeBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="NormalRectangle">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxDisabledBackgroundThemeBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CheckBoxDisabledForegroundThemeBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="CheckStates">
                                    <VisualState x:Name="Checked">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="CheckGlyph">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unchecked"/>
                                    <VisualState x:Name="Indeterminate">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="NormalRectangle">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid x:Name="Grid" Margin="{ThemeResource PhoneTouchTargetLargeOverhang}">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Grid Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Center">
                                    <Border x:Name="CheckBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="Left" Height="25.5" IsHitTestVisible="False" VerticalAlignment="Center" Width="25.5"/>
                                    <Rectangle x:Name="NormalRectangle" Fill="{ThemeResource CheckBoxBackgroundThemeBrush}" HorizontalAlignment="Center" Height="13" IsHitTestVisible="False" Visibility="Collapsed" VerticalAlignment="Center" Width="13"/>
                                    <Path x:Name="CheckGlyph" Data="M0,123 L39,93 L124,164 L256,18 L295,49 L124,240 z" Fill="{ThemeResource CheckBoxForegroundThemeBrush}" FlowDirection="LeftToRight" HorizontalAlignment="Center" Height="17" IsHitTestVisible="False" Stretch="Fill" StrokeThickness="2.5" StrokeLineJoin="Round" Visibility="Collapsed" VerticalAlignment="Center" Width="18.5"/>
                                </Grid>
                                <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Row="1" Foreground="{TemplateBinding Foreground}" FontWeight="Normal" FontSize="{ThemeResource TextStyleLargeFontSize}" FontFamily="{ThemeResource PhoneFontFamilyNormal}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>
    
    <Grid Height="Auto" VerticalAlignment="Top" Margin="0,10,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <CheckBox Name="ARStory" HorizontalAlignment="Center" Grid.Column="0" Content="Story" Style="{StaticResource CheckBoxStyleCentered}" />
        <CheckBox Name="ARPath1" HorizontalAlignment="Center" Grid.Column="1" Content="Path 1" Style="{StaticResource CheckBoxStyleCentered}" />
        <CheckBox Name="ARPath2" HorizontalAlignment="Center" Grid.Column="2" Content="Path 2" Style="{StaticResource CheckBoxStyleCentered}" />
        <CheckBox Name="ARPath3" HorizontalAlignment="Center" Grid.Column="3" Content="Path 3" Style="{StaticResource CheckBoxStyleCentered}" />
        <CheckBox Name="ARPath4" HorizontalAlignment="Center" Grid.Column="4" Content="Path 4" Style="{StaticResource CheckBoxStyleCentered}" />
    </Grid>
    


    为什么您的 XAML 无法按预期工作

    如果您检查原始复选框样式(在设计器中右键单击未设置样式的复选框 > 编辑模板 > 编辑副本),您会看到默认样式设置了复选框控件的 MinWidth

    <Setter Property="MinWidth" Value="{ThemeResource CheckBoxAndRadioButtonMinWidthSize}"/>
    

    <x:Double x:Key="CheckBoxAndRadioButtonMinWidthSize">168</x:Double>
    

    这太大了,导致复选框字形被推到网格单元格的边界之外,这就是它不可见的原因。将MinWidth="0" 设置为您的每个复选框,您将恢复您期望的原始布局。

    【讨论】:

    • 嘿,很棒的提示。谢谢!我仍然想知道为什么标记适用于 texblocks,但不适用于我的复选框。我不明白为什么它没有。无论如何,非常感谢,这比我想象的效果更好。
    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    相关资源
    最近更新 更多