WPF 无法移除 DataGrid 单元格的右边框。

huangapple go评论72阅读模式
英文:

WPF cannot remove right border for DataGrid cell

问题

我正在使用DataGrid来显示一些信息。但是,每一行的右列显示了一个额外的边框线,与DataGrid的边框不对齐。

WPF 无法移除 DataGrid 单元格的右边框。

我尝试将单元格的BorderThickness设置为0,但右边框线仍然存在。
样式XAML代码如下。

<!-- DataGrid Cell -->
<Style TargetType="{x:Type DataGridCell}" x:Key="RightCell">
    <Setter Property="BorderThickness" Value="0 0 0 0"/>
</Style>
<!--Text Block-->
<Style TargetType="{x:Type TextBlock}" x:Key="RegularTextBlock">
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="FontSize" Value="{StaticResource FontSizeSuperSmall}" />
</Style>

UI XAML代码如下。

<!-- DataGrid -->
<DataGrid Grid.Column="0" 
Margin="0,0,10,0" 
x:Name="ResultDataGrid"
ItemsSource="{Binding ResultList, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserSortColumns="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
GridLinesVisibility="All"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
HorizontalAlignment="Center"
RowHeaderWidth="0"
BorderBrush="{StaticResource CustomPureBlackBrush}"
BorderThickness="1"
Background="{StaticResource CustomPureWhiteBrush}">
<!-- Set Columns -->
<DataGrid.Columns>
<!-- First two columns have been deleted for simplicity.-->
<!-- Type Column-->
<DataGridTemplateColumn Header="Type" Width="*" IsReadOnly="True" HeaderStyle="{StaticResource FlattenRightHeader}" CellStyle="{StaticResource RightCell}">
  <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
          <Grid>
              <TextBlock Name="BuyTYpe" Text="test" Style="{StaticResource RegularTextBlock}" />
          </Grid>
      </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

有人能给我一些建议吗?谢谢。

英文:

I'm using DataGrid to show some information. However every row's right column displays an extra border line which does not align with the DataGrid border.

WPF 无法移除 DataGrid 单元格的右边框。

I've tried to set the cell BorderThickness to 0, but the right border line still exists.
The style xaml code is as below.

&lt;!-- DataGrid Cell --&gt;
&lt;Style TargetType=&quot;{x:Type DataGridCell}&quot; x:Key=&quot;RightCell&quot;&gt;
&lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;0 0 0 0&quot;/&gt;
&lt;/Style&gt;
&lt;!--Text Block--&gt;
&lt;Style TargetType=&quot;{x:Type TextBlock}&quot; x:Key=&quot;RegularTextBlock&quot;&gt;
    &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Center&quot;/&gt;
    &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Center&quot;/&gt;
    &lt;Setter Property=&quot;Background&quot; Value=&quot;Transparent&quot;/&gt;
    &lt;Setter Property=&quot;FontSize&quot; Value=&quot;{StaticResource FontSizeSuperSmall}&quot; /&gt;
&lt;/Style&gt;

The UI xaml code is as below.

&lt;!-- DataGrid --&gt;
&lt;DataGrid Grid.Column=&quot;0&quot; 
Margin=&quot;0,0,10,0&quot; 
x:Name=&quot;ResultDataGrid&quot;
ItemsSource=&quot;{Binding ResultList, UpdateSourceTrigger=PropertyChanged}&quot;
AutoGenerateColumns=&quot;False&quot;
CanUserAddRows=&quot;False&quot;
CanUserSortColumns=&quot;False&quot;
CanUserDeleteRows=&quot;False&quot;
CanUserReorderColumns=&quot;False&quot;
GridLinesVisibility=&quot;All&quot;
HorizontalScrollBarVisibility=&quot;Auto&quot;
VerticalScrollBarVisibility=&quot;Auto&quot;
HorizontalAlignment=&quot;Center&quot;
RowHeaderWidth=&quot;0&quot;
BorderBrush=&quot;{StaticResource CustomPureBlackBrush}&quot;
BorderThickness=&quot;1&quot;
Background=&quot;{StaticResource CustomPureWhiteBrush}&quot;&gt;
&lt;!-- Set Columns --&gt;
&lt;DataGrid.Columns&gt;
&lt;!-- First two columns have been deleted for simplicity.--&gt;
&lt;!-- Type Column--&gt;
&lt;DataGridTemplateColumn Header=&quot;Type&quot; Width=&quot;*&quot; IsReadOnly=&quot;True&quot; HeaderStyle=&quot; 
   {StaticResource FlattenRightHeader}&quot; CellStyle=&quot;{StaticResource RightCell}&quot; &gt;
  &lt;DataGridTemplateColumn.CellTemplate&gt;
      &lt;DataTemplate&gt;
          &lt;Grid&gt;
              &lt;TextBlock Name=&quot;BuyTYpe&quot; Text=&quot;test&quot; Style=&quot;{StaticResource RegularTextBlock}&quot; /&gt;
          &lt;/Grid&gt;
      &lt;/DataTemplate&gt;
  &lt;/DataGridTemplateColumn.CellTemplate&gt;
&lt;/DataGridTemplateColumn&gt;
&lt;/DataGrid.Columns&gt;
&lt;/DataGrid&gt;

Could someone give me some suggestions? Thanks.

答案1

得分: 0

我认为这是由于网格线引起的。尝试关闭它们:

GridLinesVisibility="None"

如果您需要这些线条,可以使用样式来代替。

英文:

I believe it is caused by the grid lines. Try turning them off:

GridLinesVisibility=&quot;None&quot;

If you need those lines, you can use styles instead.

huangapple
  • 本文由 发表于 2023年8月9日 09:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863978-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定