调整自己的用户控件的大小

时间:2013-09-23 17:46:56

标签: c# wpf windows-phone-8 wpf-controls

我正在创建一个Windows Phone应用程序,我使用自己的UserControl

<UserControl x:Class="TestApp.Negyzet"
    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}"
    Width="100" Height="100">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Rectangle x:Name="Kitoltoszin" Stroke="White" Width="100" Height="100" StrokeThickness="3" RadiusX="10" RadiusY="10" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Rectangle.Fill>
                <SolidColorBrush Color="Gray"/>
            </Rectangle.Fill>
        </Rectangle>
        <TextBlock  x:Name="Betu" Width="70" Height="70" 
                FontWeight="Bold" FontSize="42" Foreground="White" 
                HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" />
    </Grid>
</UserControl>

我将动态生成控件的Window添加到Grid

<Grid Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="NegyzetGrid" Background="Transparent">
</Grid>

生成:

for (int i = 0; i < width; i++)
{
    ColumnDefinition col = new ColumnDefinition();
    //NegyzetGrid: where I have to generate the Controls
    NegyzetGrid.ColumnDefinitions.Add(col);
    RowDefinition row = new RowDefinition();
    NegyzetGrid.RowDefinitions.Add(row);
    for (int j = 0; j < height; j++)
    {
        palya[i, j] = new Negyzet();
        palya[i, j].IsHitTestVisible = false;
        Grid.SetRow(palya[i, j], i);
        Grid.SetColumn(palya[i, j], j);
        NegyzetGrid.Children.Add(palya[i, j]);
    }
}

根据用户选择的内容,我必须生成3x3,4x4或5x5项目。我想创建一个多分辨率的应用程序,我想自动调整我自己的控件以填充整个屏幕,并且每个控件必须具有相同的大小。 (例如,如果我有900像素宽的屏幕,并且用户选择3x3,则项目宽度必须为300,如果是4x4,则为225,依此类推。)

如何设置大小?

1 个答案:

答案 0 :(得分:1)

我认为您需要在窗口中设置Horizo​​ntalAlignment和VerticalAlignment。通过这样做,您的网格将占用所有可用空间:

<Grid Grid.Row="3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" x:Name="NegyzetGrid" Background="Transparent">

</Grid>

然后在用户控件中删除UserControl声明中的width和height int,并将Rectangle上的VerticalAlignment和Horizo​​ntalAlignment属性设置为Stretch,这应该可行! :

<UserControl x:Class="ST1.Negyzet"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Rectangle x:Name="Kitoltoszin" Stroke="White" StrokeThickness="3" RadiusX="10" RadiusY="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Rectangle.Fill>
            <SolidColorBrush Color="Gray"/>
        </Rectangle.Fill>
    </Rectangle>
    <TextBlock  x:Name="Betu" Width="70" Height="70" 
               FontWeight="Bold" FontSize="42" Foreground="White" 
               HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" />
</Grid>