调整

2013-09-23 20 views
0

我创建一个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> 

我有窗口,在这里我产生了控制动态,为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

我想你需要在窗口中像这样设置Horizo​​ntalAlignment和VerticalAlignment。通过这样做,你的电网将采取一切可用的空间:

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

</Grid> 

然后在用户控件中删除的宽度和高度的int用户控件宣言和矩形设置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>