wpf WPF中网格的某些单元格的边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42609291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:01:56  来源:igfitidea点击:

Border for some cells of a grid in WPF

c#wpfxamlgrid

提问by ??? ???????

I have a data grid. I want to create several borders - not for the entire grid, but for some of the cells.

我有一个数据网格。我想创建几个边框 - 不是为整个网格,而是为一些单元格。

For example:

例如:

  1. column 2 row 2 and 3
  2. column 4 row 3 and 4.
  1. 第 2 列第 2 行和第 3 行
  2. 第 4 列第 3 行和第 4 行。

Attaching my XAML code.

附上我的 XAML 代码。

Thanks in advance for answering.

预先感谢您的回答。

<Grid Grid.Column="1">
<Grid.RowDefinitions>
    <RowDefinition Height="38"></RowDefinition>
    <RowDefinition Height="38*"></RowDefinition>
    <RowDefinition Height="38*"></RowDefinition>
    <RowDefinition Height="37*"></RowDefinition>
    <RowDefinition Height="38*"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
    <RowDefinition Height="37*"></RowDefinition>
    <RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="0.5*"></ColumnDefinition>
    <ColumnDefinition Width="0.05*"></ColumnDefinition>
    <ColumnDefinition Width="0.5*"></ColumnDefinition>
    <ColumnDefinition Width="0.05*"></ColumnDefinition>
    <ColumnDefinition Width="0.5*"></ColumnDefinition>
    <ColumnDefinition Width="0.1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="Select relay:" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"></Label>
<Label Content="Select State:" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="2" VerticalAlignment="Bottom"></Label>

<ComboBox x:Name="CBRelayNumber" VerticalAlignment="Center" Grid.Row="0" Grid.Column="2">
    <ComboBoxItem Content="ALL Relay" HorizontalAlignment="Center" />
    <ComboBoxItem Content="Relay 1" Tag="0" HorizontalAlignment="Center" />
    <ComboBoxItem Content="Relay 2" Tag="1" 

</ComboBox>
<ComboBox x:Name="CBRelayState" VerticalAlignment="Center" Grid.Row="1" Grid.Column="2">
    <ComboBoxItem Content="ON" HorizontalAlignment="Center" />
    <ComboBoxItem Content="OFF" HorizontalAlignment="Center" />
</ComboBox>

<Label Content="Select Delay (msec):" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="4" VerticalAlignment="Bottom" Grid.RowSpan="2"></Label>
<TextBox x:Name="tbAddTime" VerticalAlignment="Center" Grid.Column="4" Grid.Row="1"></TextBox>
<Button Content="Add" x:Name="AddDelay" Grid.Row="2" Grid.Column="4" VerticalAlignment="Center" Click="AddDelay_Click" ></Button>

<Button Content="Open Several Relays" x:Name="AddSeveralRelay" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" Click="AddSeveralRelay_Click" ></Button>
<Button Content="Add" x:Name="AddRelay" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" Click="AddRelay_Click" ></Button>

<Label Content="Times to perform:" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.Row="3"></Label>
<TextBox x:Name="tbLoop" Grid.Column="2" Grid.Row="3" VerticalAlignment="Center"></TextBox>
<Button Content="Save Scenario" x:Name="btnSave" Grid.Row="5" Grid.Column="0" Click="btnSave_Click"></Button>
<Button Content="Load Scenario" x:Name="btnLoad" Grid.Row="7" Grid.Column="0" Click="btnLoad_Click"/>
<Button Content="Start" x:Name="btnStart" Grid.Row="5" Grid.Column="4" Click="btnStart_Click"></Button>
<Button Content="Clear" x:Name="btnClear" Grid.Row="7" Grid.Column="4" Click="btnClear_Click"></Button>

<CheckBox x:Name="cbPostPreAllOFF" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="4" Grid.Column="2" Content="Pre-Post Relay OFF" IsChecked="True" Click="cbPostPreAllOFF_Checked"></CheckBox>

回答by ASh

add Borderinside Grid with correct Grid.Column, Grid.Row and Grid.RowSpan attributes:

Border在 Grid 中添加正确的 Grid.Column、Grid.Row 和 Grid.RowSpan 属性:

<Grid Grid.Column="1">
    <!--row definitions, columns definitions, controls-->

    <Border BorderThickness="1" BorderBrush="Green" 
            Grid.Column="2" Grid.Row="2" Grid.RowSpan="2"/>
    <Border BorderThickness="1" BorderBrush="Green" 
            Grid.Column="4" Grid.Row="3" Grid.RowSpan="2"/>
</Grid>

回答by dZZx

This did the trick for me in order to draw a border from column 0, row 1 to column+2, row+4

这对我有用,以便绘制从第 0 列第 1 行到第 + 2 行 + 4 行的边框

<Grid Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Grid.RowSpan="4">
  <Border BorderThickness="1" BorderBrush="Black" />           
</Grid>