2017-02-21 2 views
0

私はちょうどwinformsの代わりにWPFを使い始めました。固定サイズのウィンドウを作成しようとしています(画像参照)。グリッドとウィンドウのサイズの一致WPF

image 1

問題は、私はアプリを実行するたびに右下隅にはボタンとエッジとの間のスペースゼロに近い持つ、めちゃめちゃます、です。私は多くの成功なしに解決するためにグーグルで試した

<Window x:Class="UseCaseHelper.MainWindow" 
     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" 
     xmlns:local="clr-namespace:UseCaseHelper" 
     mc:Ignorable="d" 
     Title="UseCaseHelper" Height="500" Width="900"> 
    <Grid> 
     <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/> 
     <Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="809,441,0,0" VerticalAlignment="Top" Width="75"/> 

    </Grid> 
</Window> 

image 2

は、ここで(主にVisual Studioのデザイナーによって生成された)XAMLコードです(他の画像を参照してください)。うまくいけば、誰かが私がここで間違っていることを指摘することができます。

+0

両方ともにMargin = "10"を使用し、VerticalAlignment = Top/Bottom、Horizo​​ntalAlignment = Left/Rightを設定します。 – AnjumSKhan

答えて

1

私はいつもDockPanelがこれらの設定でより柔軟であると感じます。 VerticalAlighnmentと設定した余白の代わりにDockPanel.DockLeft,Right,BottomまたはTopに設定することができます。

<DockPanel LastChildFill="False"> 
     <Button DockPanel.Dock="Top" 
       Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" 
       Width="75"/> 
    <Button DockPanel.Dock="Bottom" 
      Content="Button" HorizontalAlignment="Right" Margin="0,0,10,10" Width="75"/> 
</DockPanel> 

両方のボタンにMargin = "10"を使用することもできます。ウィンドウが十分に小さい場合、この場合には、それらが重複すること

<Grid> 
    <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" 
      Width="75"/> 
    <Button Content="Button" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,10,10" Width="75"/> 
</Grid> 

注:グリッドを使用したい場合は

はしかし、あなたは次の構造を使用することができます。

別のオプションは、グリッドへRowDefinitionsColumnDefinitionsを追加することです:ウィンドウが非常に小さい場合

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 
    <Button 
      Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" 
      Width="75"/> 
    <Button Grid.Column="2" Grid.Row="2" 
     Content="Button" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,10,10" Width="75"/> 
</Grid> 

そのもパフォーマンスは、他の二つよりも優れています。

+0

ありがとうございます! DockPanelを使用して、指定されたマージンが機能します。 – Viva

関連する問題