none
Create DataGrid: All Headers, Cells and Contents Align to Center All & Width and Height Bind to Vectorel * in WPF C# RRS feed

  • Question

  • I have a observable collection which include many class content. If I set the datagrid itemsource as the observablecollection, datagrid is created well without any problems.

    My problem is alignment all cells and headers as horizontal and vertical. And also must be created auto column and width must be equal content width.

    I try to dynamicresources but not succeed entirely.

            <Style x:Key="MyAim" TargetType="{x:Type DataGrid}">
            <Setter Property="Height" Value="Auto"/>
            <Setter Property="Width" Value="Auto"/>
            <Setter Property="AlternatingRowBackground" Value="White"/>
            <Setter Property="RowBackground" Value="White"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="CanUserReorderColumns" Value="False"/>
            <Setter Property="CanUserSortColumns" Value="False"/>
            <Setter Property="CanUserResizeRows" Value="False"/>
            <Setter Property="CanUserResizeColumns" Value="False"/>
            <Setter Property="CanUserAddRows" Value="False"/>
            <Setter Property="CanUserDeleteRows" Value="False"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="HeadersVisibility" Value="Column"/>
            <Setter Property="GridLinesVisibility" Value="All" />
            <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
            <Setter Property="VerticalScrollBarVisibility" Value="Visible" />
            <Setter Property="ColumnWidth" Value="Auto"/>
            <Setter Property="AutoGenerateColumns" Value="True"/>
        </Style>

    Need to datagrid viewing like that:

    Sunday, June 16, 2019 4:53 PM

Answers


  • Hi    Yusuf Ahmet Uzundeveli,

    >>Create DataGrid: All Headers, Cells and Contents Align to Center All & Width and Height Bind to Vectorel * in WPF C#

    You can try to set the DataGridCell and HorizontalContentAlignment.

      <Window.Resources>
            <Style TargetType="DataGridCell">
                <Setter Property="TextBlock.TextAlignment" Value="Center"/>
            </Style>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </Window.Resources>

    Besides, The following links for your reference.

    Cell Alignment in DataGrid in WPF
    https://www.c-sharpcorner.com/uploadfile/dpatra/cell-alignment-in-datagrid-in-wpf/



    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 6:20 AM
    Moderator

All replies


  • Hi    Yusuf Ahmet Uzundeveli,

    >>Create DataGrid: All Headers, Cells and Contents Align to Center All & Width and Height Bind to Vectorel * in WPF C#

    You can try to set the DataGridCell and HorizontalContentAlignment.

      <Window.Resources>
            <Style TargetType="DataGridCell">
                <Setter Property="TextBlock.TextAlignment" Value="Center"/>
            </Style>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </Window.Resources>

    Besides, The following links for your reference.

    Cell Alignment in DataGrid in WPF
    https://www.c-sharpcorner.com/uploadfile/dpatra/cell-alignment-in-datagrid-in-wpf/



    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 6:20 AM
    Moderator

  • Hi    Yusuf Ahmet Uzundeveli,

    >>Create DataGrid: All Headers, Cells and Contents Align to Center All & Width and Height Bind to Vectorel * in WPF C#

    You can try to set the DataGridCell and HorizontalContentAlignment.

      <Window.Resources>
            <Style TargetType="DataGridCell">
                <Setter Property="TextBlock.TextAlignment" Value="Center"/>
            </Style>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </Window.Resources>

    Besides, The following links for your reference.

    Cell Alignment in DataGrid in WPF
    https://www.c-sharpcorner.com/uploadfile/dpatra/cell-alignment-in-datagrid-in-wpf/



    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks for your solutions.

    Best regards.
    Friday, July 5, 2019 12:59 PM