Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF datagrid text alignment in columns

WPF datagrid text alignment in columns

Scheduled Pinned Locked Moved WPF
wpfcsharpcsswcfquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elelont2
    wrote on last edited by
    #1

    Hi, i have a datagrid implemented in xaml like this:

    The grid is automatically populated (via a Binding) with two columns and some data. All is well but the text in the columns is left-aligned. Is it possible to force the text in one of the autogenerated columns to be right-aligned? Regards

    P 1 Reply Last reply
    0
    • E elelont2

      Hi, i have a datagrid implemented in xaml like this:

      The grid is automatically populated (via a Binding) with two columns and some data. All is well but the text in the columns is left-aligned. Is it possible to force the text in one of the autogenerated columns to be right-aligned? Regards

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You should be able to do this by applying a style to the DataGridColumnHeader. Off the top of my head, the following should do it:

      <Style TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="ContentTemplate">
      <Setter.Value>
      <DataTemplate>
      <TextBlock Text="{Binding}" HorizontalAlignment="Right" />
      </DataTemplate>
      </Setter.Value>
      </Setter>
      </Style>

      I was brought up to respect my elders. I don't respect many people nowadays.
      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      E 1 Reply Last reply
      0
      • P Pete OHanlon

        You should be able to do this by applying a style to the DataGridColumnHeader. Off the top of my head, the following should do it:

        <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="ContentTemplate">
        <Setter.Value>
        <DataTemplate>
        <TextBlock Text="{Binding}" HorizontalAlignment="Right" />
        </DataTemplate>
        </Setter.Value>
        </Setter>
        </Style>

        I was brought up to respect my elders. I don't respect many people nowadays.
        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        E Offline
        E Offline
        elelont2
        wrote on last edited by
        #3

        Hi and thanks for the reply. Unfortunately that did not do the trick. The column headers are hidden so i do not need to right-align them. Basically the datagrid has two columns of data. I need the DATA in ONE of the columns to be right aligned. I tried this:

         <Setter Property="CellStyle">
                    <Setter.Value>
                        <Style TargetType="DataGridCell">
                            <Setter Property="HorizontalAlignment" Value="Right"/>
        

        but that just plain righ-aligns everything in the datagrid (data in column 1 and in column 2). Is it possible to apply this to column 2 only? Note that i do not have the column definitions in my xaml since the datagrid is populated via a binding. Thanks

        P 1 Reply Last reply
        0
        • E elelont2

          Hi and thanks for the reply. Unfortunately that did not do the trick. The column headers are hidden so i do not need to right-align them. Basically the datagrid has two columns of data. I need the DATA in ONE of the columns to be right aligned. I tried this:

           <Setter Property="CellStyle">
                      <Setter.Value>
                          <Style TargetType="DataGridCell">
                              <Setter Property="HorizontalAlignment" Value="Right"/>
          

          but that just plain righ-aligns everything in the datagrid (data in column 1 and in column 2). Is it possible to apply this to column 2 only? Note that i do not have the column definitions in my xaml since the datagrid is populated via a binding. Thanks

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You can use the AutoGeneratingColumn event to handle this. Source[^].

          I was brought up to respect my elders. I don't respect many people nowadays.
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups