2

内部にいくつかの要素を持つグリッドがあります:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <TextBlock Text="SomeText" Grid.Column="0" Grid.Row="0" />
    <TextBox Grid.Column="1" Grid.Row="0" />

    <TextBlock Text="SomeText" Grid.Column="0" Grid.Row="1" />
    <TextBox Grid.Column="1" Grid.Row="1" />

    <TextBlock Text="SomeText" Grid.Column="0" Grid.Row="2" />
    <TextBox Grid.Column="1" Grid.Row="2" />
</Grid>

問題は、それがきつく見えることです:

私が持っているもの

Margin プロパティはこの問題を解決しますが、このプロパティをグリッド内の各要素に設定する必要があります。それは難しい方法です。

要素ごとではなく、一度だけマージンプロパティを設定するようなものを取得したい:

私が手に入れたいもの

4

3 に答える 3

2

Marginを暗黙的Styleに に入れることができますGrid.Resources

例えば

<Style x:Key="MarginStyle" TargetType="FrameworkElement">
     <Setter Property="Margin" Value="5"/>
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource MarginStyle}"/>
<Style TargetType="TextBlock" BasedOn="{StaticResource MarginStyle}"/>

ItemsControlを使用して共通のスタイルを適用することもできます。

例えば

<ItemsControl>
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <!-- Panel without children here -->
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition/>
          <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
      </Grid>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemContainerStyle>
    <Style TargetType="FrameworkElement">
      <Setter Property="Margin" Value="5"/>
    </Style>
  </ItemsControl.ItemContainerStyle>
  <!-- Children here -->
  <Label Grid.Row="0" Content="Field 1: "/>
  <Label Grid.Row="1" Content="Field 2: "/>
  <TextBox Grid.Column="1" Grid.Row="0"/>
  <TextBox Grid.Column="1" Grid.Row="1"/>
</ItemsControl>
于 2012-07-11T13:17:53.693 に答える
0

スタイルを作成して要素に適用してみませんか?

于 2012-07-11T13:17:44.743 に答える
0

このようなものをグリッドのリソースセクションに追加できます

    <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Margin" Value="0,5" />
        </Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Margin" Value="0,5" />
        </Style>
    </Grid.Resources>

または、2 列のグリッドが配置された垂直スタックパネルを使用することもできます。

次に、グリッドのスタイルを設定します

于 2012-07-11T13:28:42.477 に答える