我想要的功能DataGrid
1)列标题应居中对齐。
2)细胞值应保持对齐。
3)单击单元格上的编辑。
为了使标题居中对齐,我为列设置了HeaderStyle。 因此,我必须将单元格内容设置为显式左对齐。
我使用以下代码处理单击编辑 -
<DataGrid SelectionUnit="CellOrRowHeader">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Expressions" Width="*" Binding="{Binding Path=., Mode=TwoWay}">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (!cell.IsFocused)
{
cell.Focus();
}
if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
{
DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
if (dataGrid != null)
{
if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
{
if (!cell.IsSelected)
cell.IsSelected = true;
}
else
{
DataGridRow row = FindVisualParent<DataGridRow>(cell);
if (row != null && !row.IsSelected)
{
row.IsSelected = true;
}
}
}
}
}
当我点击单元格时,它看起来像这样 - the wisdom on the net
我认为选择了TextBlock,编辑元素TextBox处于编辑模式。如何删除文本周围的蓝色?