3

我正在尝试在 WPF 中获得具有某些特征的 DataGrid,但我遇到了问题。我已经制作了这些列,以便每一列都具有相同的宽度(Width="*"),并且它们填充了整个可用空间。这很好用,但现在有一个问题:当我尝试通过单击空白点(基本上没有任何文本的地方)来选择一行(这就是我需要做的)时,该行或单元格没有不被选中。

让我发布我的代码(忽略完美运行的绑定):

<DataGrid
                      ItemsSource="{Binding MyBinding}" 
                      SelectedItem="{Binding Selected}"
                      AutoGenerateColumns="False" 
                      IsReadOnly="True"
                      IsManipulationEnabled="False"
                      CanUserResizeColumns="False"
                      CanUserResizeRows="False"
                      GridLinesVisibility="Horizontal"
                      SelectionMode="Single"
                      SelectionUnit="FullRow"
                      RowHeaderWidth="0"
                      Height="Auto"
                      SelectedIndex="0"
                      >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Param1" Binding="{Binding Param1}" Width="*" />
                    <DataGridTextColumn Header="Param2" Binding="{Binding Param2}" Width="*"/>
                     ...
                    <DataGridTextColumn Header="Paramx" Binding="{Binding Paramx}" Width="*"/>
                </DataGrid.Columns>
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    </Style>
                </DataGrid.ColumnHeaderStyle>


            </DataGrid>

http://i.stack.imgur.com/c9wyp.png

如您所见,选择只影响单元格的内容,而不影响单元格本身。我希望它可以在该行的任何位置工作。

预先感谢您的帮助!

编辑:当我将 Horizo​​ntalAlignment 设置为 Stretch 时,它似乎可以工作。但是我仍然希望它居中。

EDIT2:通过这样做解决:

<DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridCell}">
                                    <Grid Background="{TemplateBinding Background}">
                                        <ContentPresenter HorizontalAlignment="Center" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.CellStyle>
4

1 回答 1

2

您确实想将 TextAlignment 属性设置为居中。但不幸的是,您无法从 FrameworkElement 访问该属性。

您最好使用 DataGridTemplateColumn 来获得所需的效果。

于 2013-07-03T12:50:04.720 回答