2010-05-26 5 views
1

탭이있는 창이 있습니다. 탭 중 하나에 다음과 같은 레이아웃이 있습니다. (실제로는 더 복잡하고 4 개의 텍스트 상자가 연속적으로 있고 더 많은 행이 있습니다.) 어떻게 세 번째 텍스트 상자의 레이블 너비가 위의 텍스트 상자 너비인지, 즉 그것들을 올바르게 정렬 시켰습니까? 문제는 WPF가 텍스트를 입력 할 때 세 번째 텍스트 상자를 넓히는 것입니다. 크기에 하드 코딩 된 숫자를 사용하면 WPF의 모든 목적을 무효화합니다. Windows Forms에서 WPF보다 10 배 더 빠릅니다.어떻게 wpf에서 텍스트 상자를 고정시킬 수 있습니까?

연속 된 작은 텍스트 상자의 각 집합에 대해 눈금을 사용하는 것보다 눈에 띄는 큰 텍스트 상자를 건너 뛰는 것보다 더 나은 방법이 있습니까?

alt text http://img28.imageshack.us/img28/3958/wpfanchor.png

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="{x:Type Label}"> 
      <Setter Property="VerticalAlignment" Value="Center"/> 
     </Style> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="VerticalAlignment" Value="Center"/> 
      <Setter Property="Margin" Value="3"/> 
     </Style> 
     <Style x:Key="SmallTextBox" TargetType="{x:Type TextBox}" 
       BasedOn="{StaticResource {x:Type TextBox}}"> 
      <Setter Property="Width" Value="50"/> 
     </Style> 
    </Window.Resources> 

    <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" 
       Width="{Binding ElementName=grid,Path=ActualWidth}" 
       Grid.IsSharedSizeScope="True"> 
     <Grid Name="grid" HorizontalAlignment="Left"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="c1"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="c2"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 

      <Label Content="Foo:"/> 
      <TextBox Grid.Column="1" Style="{StaticResource SmallTextBox}"/> 
      <Label Grid.Row="1" Content="Foobar:"/> 
      <TextBox Grid.Row="1" Grid.Column="1" 
        Style="{StaticResource SmallTextBox}"/> 
     </Grid> 

     <TextBox Grid.Row="1"/> 

     <Grid Name="grid2" HorizontalAlignment="Left"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="c1"/> 
       <ColumnDefinition Width="Auto" SharedSizeGroup="c2"/> 
      </Grid.ColumnDefinitions> 

      <Label Content="Bar:"/> 
      <TextBox Grid.Column="1" Style="{StaticResource SmallTextBox}"/> 
     </Grid> 
    </StackPanel> 
</Window> 

편집 : 4 행 하나의 그리드있을 것

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <Style TargetType="{x:Type Label}"> 
      <Setter Property="VerticalAlignment" Value="Center"/> 
     </Style> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="VerticalAlignment" Value="Center"/> 
      <Setter Property="Margin" Value="3"/> 
     </Style> 
     <Style x:Key="SmallTextBox" TargetType="{x:Type TextBox}" 
       BasedOn="{StaticResource {x:Type TextBox}}"> 
      <Setter Property="Width" Value="50"/> 
     </Style> 
    </Window.Resources> 

    <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" 
       Width="{Binding ElementName=grid,Path=ActualWidth}"> 
     <Grid Name="grid"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 

      <Label Content="Foo:"/> 
      <TextBox Grid.Column="1" Style="{StaticResource SmallTextBox}"/> 
      <Label Grid.Row="1" Content="Foobar:"/> 
      <TextBox Grid.Row="1" Grid.Column="1" 
        Style="{StaticResource SmallTextBox}"/> 
      <TextBox Grid.Row="2" Grid.ColumnSpan="2"/> 
      <Label Grid.Row="3" Content="Bar:"/> 
      <TextBox Grid.Row="3" Grid.Column="1" 
        Style="{StaticResource SmallTextBox}"/> 
     </Grid> 
    </StackPanel> 
</Window> 

답변

0
<TextBox Grid.Row="1" Grid.ColumnSpan="2" /> 
0

: 여기 줄리앙 Lebosquain의 답변에 따라 솔루션입니다. 그리고 3 행에 ColumnSpan이 있습니다. 또한 SharedSizeGroups이 필요 없다는 것을 의미합니다.

<Grid Name="grid" HorizontalAlignment="Left"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="Auto" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 

     <Label Content="Foo:"/> 
     <TextBox Grid.Column="1" Style="{StaticResource SmallTextBox}"/> 
     <Label Grid.Row="1" Content="Foobar:"/> 
     <TextBox Grid.Row="1" Grid.Column="1" 
       Style="{StaticResource SmallTextBox}"/> 

     <TextBox Grid.Row="2" Grid.ColumnSpan="2" /> 

     <Label Content="Bar:"/> 
     <TextBox Grid.Row="3" Grid.Column="1" Style="{StaticResource SmallTextBox}"/> 
    </Grid> 
+0

이것은 작동하지 않습니다. 세 번째 텍스트 상자에 일부 텍스트를 입력하면 텍스트가 더 넓어집니다. –

1

나는 거꾸로 생각합니다. 모든 것을 엉망으로 만드는 가장 큰 텍스트 상자는 아닙니다. 작은 글 상자는 크기가 고정되어있어 가장 큰 글씨처럼 보이지 않습니다. 여기에 모순이 있습니다. 스택 패널을 사용하면 동일한 동작을하는 "내 아이의 너비"를 사용하지만 스택 패널이 커지지 않도록 할 수 있습니다.

시각적 트리의 어느 곳에서나 너비를 지정하거나 크기 조절 동작이 Grid와 같은 전체 공간을 차지하는 컨트롤을 사용해야합니다. 작은 텍스트 상자가 더 이상 고정 된 크기를 가지고 있지,

  • 를 사용하여 하나의 내부 그리드 및 큰 텍스트 상자 Grid.ColumnSpan="2"을 갖는

    개인적으로, 나는이 솔루션과 함께 갈 것입니다.

  • 첫 번째 열에 Width="Auto"을 적용하고 두 번째 열에 Width="*"을 적용하십시오.
  • 기존의 StackPanel을 첫 번째 열이 고정 또는 별표 크기 인 Grid으로 바꿉니다 (창의 크기가 조정될 때 크기가 조정될 수 있도록). 이 첫 번째 열에 내부 격자를 배치하십시오.
+0

감사합니다. 두 번째 열의 너비를 *로 설정하면 속임수가 적용됩니다. 작은 결함으로 작동합니다. 단일 격자를 사용할 수 있지만 Width = "{Binding ElementName = grid, Path = ActualWidth}"와 함께 stackpanel을 유지해야합니다. –

관련 문제