Chcę dopasować moje kolumny w DataGrid. Jak mam to zrobić? Używam również poziomegoContentalignment = "Center", ale nie działa. Dziękujemy za pomoc

    <DataGrid Margin="0,69,0,0" x:Name="dgv_Useradmin" FlowDirection="RightToLeft"
              CanUserDeleteRows="False" CanUserResizeRows="False" CanUserReorderColumns="False" ColumnWidth="*" Background="Azure"
              FontFamily="B Roya" FontWeight="Bold" FontStretch="UltraCondensed" BorderBrush="#29bb89" OpacityMask="#e84545" HorizontalAlignment="Center"
              CanUserAddRows="False" AlternatingRowBackground="Bisque" IsReadOnly="True" AutoGenerateColumns="False" HorizontalContentAlignment="Center"
              RowDetailsVisibilityMode="Visible" SelectedIndex="0" LoadingRow="Dgv_Useradmin_OnLoadingRow">

        <!--DataColumn-->
        <DataGrid.Columns>
            <DataGridTextColumn Header="RN" Width="65" Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={convertor:RowToIndexConverter}}"/>
            <DataGridTextColumn Header="LogID" Binding="{Binding LogID}" Visibility="Hidden"/>
            <DataGridTextColumn Header="Username" Binding="{Binding Username }"/>
            <DataGridTextColumn Header="Pass" Binding="{Binding PassWord}" Visibility="Hidden"/>
            <DataGridTextColumn Header="Number" Binding="{Binding NumberPhone}"/>
        </DataGrid.Columns>
-1
Trosam 15 kwiecień 2021, 12:45

2 odpowiedzi

Najlepsza odpowiedź

Pokazane dla jednej kolumny. Dla reszty kolumn, to samo jest prawdziwe.

        <DataGrid.Columns>
            <DataGridTemplateColumn Header="RN" Width="65">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={convertor:RowToIndexConverter}}"
                                   TextAlignment="Center"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <!--If editing is envisaged-->
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={convertor:RowToIndexConverter}}"
                                 HorizontalContentAlignment="Center"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="LogID" Binding="{Binding LogID}" Visibility="Hidden"/>
            <DataGridTextColumn Header="Username" Binding="{Binding Username }"/>
            <DataGridTextColumn Header="Pass" Binding="{Binding PassWord}" Visibility="Hidden"/>
            <DataGridTextColumn Header="Number" Binding="{Binding NumberPhone}"/>
        </DataGrid.Columns>
0
EldHasp 15 kwiecień 2021, 14:20

Aby wyśrodkować nagłówek, musisz dać mu styl:

                <DataGridTemplateColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    </Style>
                </DataGridTemplateColumn.HeaderStyle>
0
EldHasp 15 kwiecień 2021, 15:09