Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调整自己的用户控件的大小_C#_Wpf_Windows Phone 8_Wpf Controls - Fatal编程技术网

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

C# 调整自己的用户控件的大小,c#,wpf,windows-phone-8,wpf-controls,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.microso

我正在创建一个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>
生成:

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]);
    }
}
for(int i=0;i
根据用户的选择,我必须生成3x3、4x4或5x5项。我想创建一个多分辨率的应用程序,我想自动调整我自己控件的大小以填充整个屏幕,每个控件的大小必须相同。(例如,如果我有一个900像素宽的屏幕,用户选择3x3,则项目宽度必须为300,如果为4x4,则为225,依此类推。)


如何设置这样的大小?

我想您需要在窗口中设置这样的水平对齐和垂直对齐。这样,网格将占用所有可用空间:

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

</Grid>

然后在用户控件中删除UserControl声明中的宽度和高度,并在要拉伸的矩形上设置垂直对齐和水平对齐属性,这应该可以做到

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

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