启用文本换行时,在编辑模式下从DataGridCell中删除边框

时间:2015-04-19 12:47:03

标签: c# wpf wpfdatagrid

我有一个数据网格,我不希望网格线随时显示。

我几乎可以使用以下代码:

<DataGrid  
        AutoGenerateColumns="False" Height="Auto" 
        Name="dataGrid1" Width="504" 
        ItemsSource="{Binding SourceCollection}" 
        DockPanel.Dock="Top" GridLinesVisibility="None" 
        CanUserSortColumns="False" 
        ColumnWidth="Auto" HeadersVisibility="None" 
        FontSize="16" FontFamily="Tahoma" MinRowHeight="30">
 <DataGrid.Columns>
     <DataGridTextColumn Binding="{Binding index}" />
     <DataGridTextColumn Binding="{Binding des}" Width="20*">
     </DataGridTextColumn>
 </DataGrid.Columns>
 <DataGrid.CellStyle>
     <Style>
         <Style.Triggers>
             <Trigger Property="DataGridCell.IsSelected" Value="True">
                 <Setter Property="DataGridCell.Background" Value="White" />
                 <Setter Property="DataGridCell.Foreground" Value="Black" />
                 <Setter Property="DataGridCell.BorderBrush" Value="White" />
                 <Setter Property="DataGridCell.BorderThickness" Value="1" />
             </Trigger>                  
         </Style.Triggers>
     </Style>
 </DataGrid.CellStyle>
 <DataGrid.RowStyle>
     <Style TargetType="DataGridRow">
         <Style.Triggers>
             <Trigger Property="IsSelected" Value="True">
                 <Setter Property="Background" Value="White" />
     </Trigger>
         </Style.Triggers>
     </Style>
 </DataGrid.RowStyle>
<DataGrid>

enter image description here

但是,只要我为第二列启用文本换行,就会在编辑模式下在单元格周围显示一条蓝线

<DataGridTextColumn.ElementStyle>
<Style>
    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
    <Style>
        <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
    </Style>
</DataGridTextColumn.EditingElementStyle>

Window after enable text wrap

那么如何在保持TextWrap选项的同时从编辑模式的DataGridCell中删除蓝线呢?

1 个答案:

答案 0 :(得分:1)

对于elementstyle,您可以使用TextBlock,对于Element编辑样式,您可以使用文本框并将其视为0或

<DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}" >                          
                        <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                    </Style>                     
                </DataGridTextColumn.ElementStyle>
                <DataGridTextColumn.EditingElementStyle>
                    <Style  TargetType="{x:Type TextBox}">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="TextWrapping" Value="Wrap" />
                    </Style>
                </DataGridTextColumn.EditingElementStyle>