2011-01-08 11 views
5

最近私の友人にWPFレイアウトの問題を助けようとしていましたが、どうすればそれを稼働させるのか分かりません。このような単純なことは、私はここで豊富な知識を活用すると思った:彼が望んでいるのは、groupbox1がmaxwidthの値に自動サイズ変更してから、グループボックスの右側のスペースが大きくなる一方、このシンプルさを保つために、私はちょうど今の状況のいくつかのサンプルコードを投稿するつもりです:)もし誰かが状況に照らすためにいくつかの光を持っている場合は応答してください。みんな、ありがとう!グループボックスを左に固定する方法はありますが、グループボックスを最大幅に伸ばすには

<Window x:Class="GroupBoxTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="147" Width="525"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="151*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="148" /> 
     <ColumnDefinition Width="355*" /> 
    </Grid.ColumnDefinitions> 
    <GroupBox Header="groupBox1" Margin="14,12,41,8" Name="groupBox1" MaxWidth="450" Grid.Column="1"> 
     <Grid /> 
    </GroupBox> 
    <GroupBox Header="groupBox2" Margin="12,12,13,8" Name="groupBox2"> 
     <Grid /> 
    </GroupBox> 
</Grid> 

答えて

1

移動MaxWidthの= "450" とのGroupBoxから削除します。 GroupBoxのために動作しますColumnDefinitionにMaxWidth="450"を設定

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="148" /> 
     <ColumnDefinition Width="*" MaxWidth="450" /> 
    </Grid.ColumnDefinitions> 
    <GroupBox 
     Name="groupBox1" 
     Header="groupBox1" 
     Margin="14,12,41,8" 
     Grid.Column="1"> 
    </GroupBox> 
    <GroupBox Header="groupBox2" Margin="12,12,13,8" Name="groupBox2"> 
    </GroupBox> 
</Grid> 
+0

AWESOME !!それは完璧に動作します!本当にありがとう!! – Dan

+0

@ Dan:グリッド列が実際に予想どおりにサイズ調整される必要はないことにご注目いただきました。このソリューションでは、列内のすべての要素がグループボックスだけでなく450の幅まで広がります。 –

0

あなたはこのようなスタイルを使用して回避策を作成することができます。ColumnDefinitionへ

<GroupBox Header="groupBox1" Margin="14,12,41,8" Name="groupBox1" MaxWidth="450" Grid.Column="1"> 
    <GroupBox.Style> 
     <Style TargetType="GroupBox"> 
      <Style.Triggers> 
       <Trigger Property="ActualWidth" Value="450"> 
         <Setter Property="Width" Value="450"/> 
         <Setter Property="HorizontalAlignment" Value="Left"/> 
        </Trigger>  
       </Style.Triggers> 
      </Style>  
     </GroupBox.Style> 
    <Grid /> 
</GroupBox> 
+0

おかげH.B.のソースとして答えのために。ウィンドウが元に戻って収縮すると、グループボックスは消えません。私はトリガーで遊んでいないので、これ以上詳しく見ていませんが、応答にはとても感謝しています! – Dan

+0

ああ、私は戻ってサイジングするときにそこに発生したクリッピングを見ていない、それは実際には他の方法も働いていたように見えた、それを修正する方法がある必要があります... –

2

<ColumnDefinition Width="355*" MaxWidth="450"/> 

カラム1内の他の要素は、あなたがGroupBoxためHorizontalAlignment="Left"を設定し、同じ列内の他の要素にWidthをバインドすることができ、さらに450よりもストレッチする必要がある場合。 ColumnDefinitionため

ActualWidthは、そうでない場合は、我々が使用している可能性があり、依存関係プロパティではありませんそのバインディング

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="151*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="148" /> 
     <ColumnDefinition Width="355*" /> 
    </Grid.ColumnDefinitions> 
    <Rectangle Name="sizeElement" Fill="Transparent" Margin="14,12,41,8" Grid.Column="1"/> 
    <GroupBox Header="groupBox1" Margin="14,12,41,8" Name="groupBox1" MaxWidth="450" Grid.Column="1" 
       HorizontalAlignment="Left" 
       Width="{Binding ElementName=sizeElement, Path=ActualWidth}"> 
     <Grid /> 
    </GroupBox> 
    <GroupBox Header="groupBox2" Margin="12,12,13,8" Name="groupBox2"> 
     <Grid /> 
    </GroupBox> 
</Grid> 
関連する問題