简体   繁体   English

在布局Windows Phone的网格内将UserControl居中

[英]Center UserControl within a grid in a Layout Windows Phone

Good day, How can i center a UserControl to fit dead center of a grid in another Layout. 美好的一天,如何将UserControl居中以适合另一个布局中网格的死点。 I have created a UserControl with an Image Button in its center. 我创建了一个UserControl,其中心有一个Image Button。 when i try to add this UserControl and center it in a gridlayout in another layout, it becomes very mis-aligned. 当我尝试添加此UserControl并将其居中放置在另一个布局的gridlayout中时,它变得非常不对齐。 How can i achieve this?.. ManyThanks 我怎样才能做到这一点?

My UserControl XAML: 我的UserControl XAML:

<UserControl x:Class="MyApp.ImageFullScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}" Height="639.848">


        <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,-10" Grid.RowSpan="4">
            <Grid.RowDefinitions>
                <RowDefinition Height="680"/>
            </Grid.RowDefinitions>
            <Canvas x:Name="ViewFinderCanvas" Margin="-12,-12,-471,51">
                <!--Camera viewfinder -->
                <Canvas.Background>
                    <VideoBrush x:Name="cameraviewfinder"/>
                </Canvas.Background>
                <Button x:Name="add_button" Canvas.Left="168" Canvas.Top="282" HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="0.5" RenderTransformOrigin="0.576,-0.098">
                    <Grid>
                        <Image x:Name="AddButtonImage" Source="/Assets/Images/add_button.png"/>
                    </Grid>
                </Button>
            </Canvas>
        </Grid>

</UserControl>

Main_Layout XAML: Main_Layout XAML:

     <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="40"/>
                <ColumnDefinition/>
                <ColumnDefinition Width="40"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="48*"/>
                <RowDefinition Height="680*"/>
                <RowDefinition Height="48*"/>
            </Grid.RowDefinitions>

            <TextBox Grid.Column="1" HorizontalAlignment="Left" Height="62" Grid.Row="2" TextWrapping="Wrap" Text="page" VerticalAlignment="Top" Width="70" Margin="386,0,-17,-14" FontSize="10" Grid.ColumnSpan="2"/>

            <!--ContentPanel - place additional content here-->
            <Grid x:Name="ContentPanel" Margin="12,0,12,0" Grid.RowSpan="3"/>


            <Grid Grid.Column="1" HorizontalAlignment="Left" Height="673" Grid.Row="1" VerticalAlignment="Top" Width="400">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

<!--User control here-->
                <local:ImageFullScreen x:Name="ImageFull" Grid.Column="1" Grid.Row="1" Loaded="ImageFullScreen_Loaded" Width="400" HorizontalAlignment="Left" RenderTransformOrigin="0.424,0.481"/>
            </Grid>

        </Grid>

In your MainPage.xaml, you have the HorizontalAlignment set to left and the VerticalAlignment not set at all. 在MainPage.xaml中,您将“ Horizo​​ntalAlignment”设置为左侧,而“ VerticalAlignment”则完全没有设置。 Try setting both to center. 尝试将两者都设置为居中。

A few things. 一些东西。

  • By default the Grid control will take up as much space as it is given. 默认情况下,Grid控件将占用给定的空间。 Because of this do not give controls a Height/Width that you want to be "FullScreen". 因此,不要给控件一个想要成为“ FullScreen”的高度/宽度。 Simply place them so they have no restrictions. 只需放置它们,使其不受限制。
  • By default the Grid control will center the content it is given. 默认情况下,网格控件将居中显示给定的内容。 This changes when items placed in the control have a HorizontalAlignment set to Left/Right or a VerticalAlignment set to Top/Bottom 当放置在控件中的项目的Horizo​​ntalAlignment设置为Left / Right或VerticalAlignment设置为Top / Bottom时,此更改
  • Live the Grid, the Canvas control will take up as much space as it is given. 使用网格,Canvas控件将占用给定的空间。

Try the following for your UserControl 为您的UserControl尝试以下操作

<UserControl x:Class="MyApp.ImageFullScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}" Height="639.848">

    <Canvas x:Name="ViewFinderCanvas" Margin="10">
        <!--Camera viewfinder -->
        <Canvas.Background>
            <VideoBrush x:Name="cameraviewfinder"/>
        </Canvas.Background>
        <Button x:Name="add_button" Canvas.Left="168" Canvas.Top="282" Opacity="0.5">
            <Image x:Name="AddButtonImage" Source="/Assets/Images/add_button.png"/>
        </Button>
    </Canvas>
</UserControl>

And the following for your Page 以下是您的页面

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid Background="Transparent">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--User control here-->
    <local:ImageFullScreen x:Name="ImageFull" Loaded="ImageFullScreen_Loaded" 
           Grid.Column="1" Grid.Row="1" Height="673" Width="400"/>
    <TextBox Grid.Column="2" Grid.Row="2" Margin="-10" TextWrapping="Wrap" Text="page" Width="70" FontSize="10" />
</Grid>

Notice that I stripped a lot out. 注意,我剥离了很多东西。 I can do this because the controls being used make it that easy! 我可以这样做是因为所使用的控件使操作变得如此简单!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM