如何在Silverlight中模仿WinForm锚点?

时间:2013-12-13 01:19:44

标签: c# silverlight

使用Silverlight,我有一个按钮,我想在其中添加两个TextBlock:一个完全位于其父容器中心,另一个位于其父容器的右下角(见下图)。在古老的WinForms中,使用锚点很容易实现这一点。但是,使用有限数量的Silverlight容器,我无法获得相同的结果。

有关如何实现这一目标的任何想法或建议?我正在尝试为此设置合适的容器,并确保一切都完美对齐。

请注意,文本可能超过1个字符,X和0可能会略微重叠(换句话说,我不能使用3行网格)。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以在网格中将元素堆叠在一起,如下所示:

    <Border BorderThickness="2" BorderBrush="Blue" Width="200" Height="200">
        <Grid>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="X" />
            <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="0" />
        </Grid>
    </Border>

修改

此示例使用自定义按钮样式来实现发布结果:

    <Button Width="200" Height="200" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" BorderThickness="6" BorderBrush="SteelBlue" Background="White" Padding="16">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="FocusStates">
                                        <VisualState x:Name="Unfocused"/>
                                        <VisualState x:Name="Focused"/>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="MouseOver"/>
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="Pressed"/>
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <DoubleAnimation Duration="0" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="1"/>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Border x:Name="Background" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" />
                                <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>
                                <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" Opacity="0" IsHitTestVisible="false" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
        <Grid>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="X" FontSize="144" FontWeight="Bold" Foreground="SteelBlue" />
            <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="0" FontSize="24" Foreground="SteelBlue" />
        </Grid>
    </Button>