C# 在DataGrid中单击“编辑”可选择单元格的文本块

C# 在DataGrid中单击“编辑”可选择单元格的文本块,c#,wpf,datagrid,C#,Wpf,Datagrid,我想在我的DataGrid中使用的功能 1) 列标题应居中对齐 2) 单元格值应左对齐 3) 在单元格上单击“编辑” 对于要居中对齐的标题,我设置了列的标题样式。 因此,我必须将单元格内容明确设置为左对齐 我使用以下代码处理单击编辑- <DataGrid SelectionUnit="CellOrRowHeader"> <DataGrid.Resources> <Style TargetType="{x:Type DataGridCe

我想在我的
DataGrid中使用的功能

1) 列标题应居中对齐

2) 单元格值应左对齐

3) 在单元格上单击“编辑”

对于要居中对齐的标题,我设置了列的标题样式。 因此,我必须将单元格内容明确设置为左对齐

我使用以下代码处理单击编辑-

 <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;
              }
           }
        }
     }
  }

私有void DataGridCell_PreviewMouseLeftButtonDown(对象发送器,鼠标按钮ventargs e)
{
DataGridCell=发送方为DataGridCell;
如果(!cell.IsFocused)
{
cell.Focus();
}
if(cell!=null&&!cell.IsEditing&&!cell.IsReadOnly)
{
DataGrid DataGrid=FindVisualParent(单元格);
if(dataGrid!=null)
{
if(dataGrid.SelectionUnit!=DataGridSelectionUnit.FullRow)
{
如果(!cell.IsSelected)
cell.IsSelected=true;
}
其他的
{
DataGridRow行=FindVisualParent(单元格);
if(row!=null&&!row.IsSelected)
{
row.IsSelected=true;
}
}
}
}
}
当我点击单元格时,它看起来像这样-


我认为TextBlock已被选中,编辑元素TextBox处于编辑模式。如何删除文本周围的蓝色

这是你想关注的最后一个问题还是全部?我建议对问题进行编辑,不要问多个问题,而是关注一个问题。这将提高获得答案的机会。将其他问题用于其他相关问题。是。我的重点是最后一个问题。将编辑我的问题。谢谢你的建议。这是你想关注的最后一个问题还是全部?我建议对问题进行编辑,不要问多个问题,而是关注一个问题。这将提高获得答案的机会。将其他问题用于其他相关问题。是。我的重点是最后一个问题。将编辑我的问题。谢谢你的建议。