XAML C#逐个窗口中元素的动态位置

时间:2014-11-03 11:11:31

标签: c# wpf xaml grid resize

我是C#和XAML的新手,我对元素的动态调整大小/位置有困难。 我想用这个模式创建3x3网格:

text  |  text  |  grid
image |  image |  same grid of above
text  |  text  |  same grid of above

我写了以下代码:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Microsoft.Samples.Kinect.BodyBasics.MainWindow"
        Title="Body tracking" 
        Height="Auto" Width="Auto" 
        Loaded="MainWindow_Loaded"
        Closing="MainWindow_Closing">
    <Window.Resources>
        <SolidColorBrush x:Key="MediumGreyBrush" Color="#ff6e6e6e" />
        <SolidColorBrush x:Key="KinectPurpleBrush" Color="#ff52318f" />
        <SolidColorBrush x:Key="KinectBlueBrush" Color="#ff00BCF2" />
    </Window.Resources>
    <Grid Margin="10,1,10,1" ShowGridLines="True" Height="Auto" Width="Auto" VerticalAlignment="Bottom">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition  Width="Auto"/>
            <ColumnDefinition  Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock  MinHeight="25" MinWidth="25" Grid.Row="0" Grid.Column="0" Foreground="{StaticResource MediumGreyBrush}" FontFamily="Segoe UI" FontSize="18"><Run Text="Body Tracking"/>
        </TextBlock>

        <Viewbox Grid.Row="1" Grid.Column="0">
            <Border x:Name="borderSkeleton" BorderThickness="7" Height="Auto" Width="Auto">
                <Image Source="{Binding ImageSource}" Stretch="UniformToFill" />
            </Border>
        </Viewbox>

        <Image Grid.Column="1" Grid.Row="1" x:Name="imageReference" Height="Auto" Width="Auto" Stretch="UniformToFill"/>

        <StatusBar MinHeight="25" MaxHeight="25" x:Name="statusBar" Grid.Row="2" Grid.Column="0" Background="White" Foreground="{StaticResource MediumGreyBrush}">
            <StatusBarItem Content="{Binding StatusText}" />
        </StatusBar>
        <Label x:Name="lblIstruction" Grid.Row="0" Grid.Column="1"  Content="Reference Pose: please,  position yourself as picture" Width="Auto" Height="Auto" FontSize="16"/>
        <Grid Grid.Row="1" Grid.Column="2" Grid.RowSpan="3" Width="Auto" Height="Auto">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Grid.Row="0" x:Name="lblFeedback1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="1" x:Name="lblFeedback2" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="2" x:Name="lblFeedback3" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="3" x:Name="lblFeedback4" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="4" x:Name="lblFeedback5" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="5" x:Name="lblFeedback6" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="6" x:Name="lblFeedback7" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="7" x:Name="lblFeedback8" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="8" x:Name="lblFeedback9" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="9" x:Name="lblFeedback10" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="10" x:Name="lblFeedback11" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="11" x:Name="lblFeedback12" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="12" x:Name="lblFeedback13" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="13" x:Name="lblFeedback14" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Label Grid.Row="14" x:Name="lblFeedback15" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        </Grid>
        <Label Grid.Row="2" Grid.Column="1" x:Name="lblSummaryFeedback"  FontSize="18" FontWeight="Bold"  Width="Auto" Height="Auto" />
    </Grid>
</Window>

但我看不到所有的元素。此外,当我调整窗口大小时,网格会被剪切或调整大小。

1 个答案:

答案 0 :(得分:2)

我通过将带有图像的行和列设置为*而不是自动来解决此问题。我还为所有图像添加了viewBox,以便保持图像比例并且不会缩小图像。

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Microsoft.Samples.Kinect.BodyBasics.MainWindow"
    Title="Body tracking" 
    Height="Auto" Width="Auto" 
    Loaded="MainWindow_Loaded"
    Closing="MainWindow_Closing">
<Window.Resources>
    <SolidColorBrush x:Key="MediumGreyBrush" Color="#ff6e6e6e" />
    <SolidColorBrush x:Key="KinectPurpleBrush" Color="#ff52318f" />
    <SolidColorBrush x:Key="KinectBlueBrush" Color="#ff00BCF2" />
</Window.Resources>
<Grid Margin="10,1,10,1" ShowGridLines="True" VerticalAlignment="Bottom">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition  Width="*"/>
        <ColumnDefinition  Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBlock  MinHeight="25" MinWidth="25" Grid.Row="0" Grid.Column="0" Foreground="{StaticResource MediumGreyBrush}" FontFamily="Segoe UI" FontSize="18"><Run Text="Body Tracking"/></TextBlock>

    <Viewbox Grid.Row="1" Grid.Column="0">
        <Border x:Name="borderSkeleton" BorderThickness="7" Height="Auto" Width="Auto">
            <Image Source="{Binding ImageSource}" Stretch="UniformToFill" />
        </Border>
    </Viewbox>

    <Viewbox Grid.Row="1" Grid.Column="1">
        <Image x:Name="imageReference"  Stretch="UniformToFill" />
    </Viewbox>

    <StatusBar MinHeight="25" MaxHeight="25" x:Name="statusBar" Grid.Row="2" Grid.Column="0" Background="White" Foreground="{StaticResource MediumGreyBrush}">
        <StatusBarItem Content="{Binding StatusText}" />
    </StatusBar>
    <Label x:Name="lblIstruction" Grid.Row="0" Grid.Column="1"  Content="Reference Pose: please,  position yourself as picture" Width="Auto" Height="Auto" FontSize="16"/>
    <Grid Grid.Row="1" Grid.Column="2" Grid.RowSpan="3" Width="Auto" Height="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" x:Name="lblFeedback1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="1" x:Name="lblFeedback2" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="2" x:Name="lblFeedback3" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="3" x:Name="lblFeedback4" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="4" x:Name="lblFeedback5" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="5" x:Name="lblFeedback6" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="6" x:Name="lblFeedback7" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="7" x:Name="lblFeedback8" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="8" x:Name="lblFeedback9" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="9" x:Name="lblFeedback10" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="10" x:Name="lblFeedback11" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="11" x:Name="lblFeedback12" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="12" x:Name="lblFeedback13" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="13" x:Name="lblFeedback14" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Label Grid.Row="14" x:Name="lblFeedback15" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid>
    <Label Grid.Row="2" Grid.Column="1" x:Name="lblSummaryFeedback"  FontSize="18" FontWeight="Bold"  Width="Auto" Height="Auto" />
</Grid>