2008-11-25 71 views
16

我現在正在玩弄WPF,現在我想知道典型的數據入口窗口(20多個文本框和東西)的佈局是什麼。WPF DataEntry窗口的最佳實踐

使用這樣的(基本樣本)網格對象

ATM我真的

<Grid Margin="2,2,2,2"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"></ColumnDefinition> 
      <ColumnDefinition Width="*"></ColumnDefinition> 
     </Grid.ColumnDefinitions> 

     <Grid.RowDefinitions > 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
      <RowDefinition Height="Auto"></RowDefinition> 
     </Grid.RowDefinitions> 

      <Label Grid.Row="0" Grid.Column="0">Vorname:</Label> 
      <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=Surname, UpdateSourceTrigger=PropertyChanged}" ></TextBox> 

      <Label Grid.Row="1" Grid.Column="0">Nachname:</Label> 
      <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=ChristianName, UpdateSourceTrigger=PropertyChanged}"></TextBox> 

      <Label Grid.Row="2" Grid.Column="0">Strasse (Wohnsitz):</Label> 
      <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=Street1, UpdateSourceTrigger=PropertyChanged}"></TextBox> 

      <Label Grid.Row="3" Grid.Column="0">Ort (Wohnsitz):</Label> 
      <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Path=Town1, UpdateSourceTrigger=PropertyChanged}"></TextBox> 

      <Label Grid.Row="4" Grid.Column="0">Postleitzahl (Wohnsitz):</Label> 
      <TextBox Grid.Row="4" Grid.Column="1" Text="{Binding Path=PostalCode1, UpdateSourceTrigger=PropertyChanged}"></TextBox> 

      <Label Grid.Row="5" Grid.Column="0">Bundesland (Wohnsitz):</Label> 
      <TextBox Grid.Row="5" Grid.Column="1" Text="{Binding Path=State1, UpdateSourceTrigger=PropertyChanged}"></TextBox> 

      <Label Grid.Row="6" Grid.Column="0">Land (Wohnsitz):</Label> 
      <TextBox Grid.Row="6" Grid.Column="1" Text="{Binding Path=Country1, UpdateSourceTrigger=PropertyChanged}"></TextBox> 

      <Label Grid.Row="7" Grid.Column="0">Zusatz (Wohnsitz):</Label> 
      <TextBox Grid.Row="7" Grid.Column="1" Text="{Binding Path=AdditionalAdrInfo1, UpdateSourceTrigger=PropertyChanged}"></TextBox> 

    </Grid> 

基本上這滿足我所有的佈局的需要,但如果我想改變一些東西,比如第3行中添加一個新的文本框?

目前我必須改變每一個Grid.Row屬性大於3,但不能成爲預期的WPF方式!?

其他人如何佈局複雜的數據入口窗口?

TIA

回答

3

有人使用嵌套StackPanel s到「解決」這個問題,但恕我直言,只是帶來了另一個問題(代碼膨脹)。我認爲解決這個問題的最好方法是編寫自己的小組,連續列出兒童。我這樣做是對以前的項目,它有許多優點:

  • 更具可讀性和簡潔的XAML
  • 易於維護XAML
  • 更快的性能

用法看起來是這樣的:

<local:FieldPanel> 
    <Label>Field 1:</Label> 
    <TextBox/> 

    <Label>Field 2:</Label> 
    <TextBox/> 

    <Label>Field 3:</Label> 
    <TextBox/> 
</local:FieldPanel>