在WPF中的某些网格单元格上应用边框样式

时间:2016-01-04 14:04:44

标签: c# wpf properties grid

我想将以下样式应用于网格的某些单元格:

<Border BorderBrush="#808080" BorderThickness="1,1,0,0" Grid.Column="3" >
    <Border BorderBrush="#404040" BorderThickness="1,1,0,0" Width="Auto">
        <Border BorderBrush="#FFFFFF" BorderThickness="0,0,1,1" Width="Auto">
            <Border BorderBrush="#DCE6F4" BorderThickness="0,0,1,1" Width="Auto">
            </Border>
        </Border>
    </Border>
</Border>

现在我在第一行Grid.Colum="3"指定单元格。但是要为所有细胞执行此操作,我必须复制粘贴20次。有没有简短的方法将我的风格应用于我选择的所有控件或单元格(例如单元格1,7,9或标签1,标签9和标签10?

我是wpf的新手,请温柔:)

1 个答案:

答案 0 :(得分:2)

假设您要在网格中放置标签,可以创建ControlTemplate并使用Template="{StaticResource BorderLabel}"将其指定给所需的标签:

<Grid Margin="5">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>

  <Grid.Resources>
    <ControlTemplate TargetType="Label" x:Key="BorderLabel">
      <Border BorderBrush="#808080" BorderThickness="1,1,0,0" >
        <Border BorderBrush="#404040" BorderThickness="1,1,0,0" Width="Auto">
          <Border BorderBrush="#FFFFFF" BorderThickness="0,0,1,1" Width="Auto">
            <Border BorderBrush="#DCE6F4" BorderThickness="0,0,1,1" Width="Auto">
              <ContentControl Content="{TemplateBinding Content}" />
            </Border>
          </Border>
        </Border>
      </Border>
    </ControlTemplate>

  </Grid.Resources>

  <Label Grid.Column="0" Content="MyText" />
  <Label Grid.Column="1" Content="MyText" Template="{StaticResource BorderLabel}" />
</Grid>

这就是它的样子:

Result