GridViewColumnHeader not spanning whole ListView width












1















I am having trouble with the layout of a ListView / GridView combination in my WPF project. The columnheaders are just not spanning the whole width of the parent ListView. I tried to find documentation everywhere but didn't find any clues about the underlying containers / control templates.



I guess the standard control template of the header contains something that I am not taking care of, I just cannot wrap my head around it.



The (simplified) ListView has a few pixel space to the left and to the right of the row containing the headers, where the background of the parent ListView is visible:



https://i.imgur.com/XijV88a.jpg



Question: How can I make the columnheaders span the whole width of the ListView and make the few pixels space disappear?



My XAML:



<ListView Padding="0"
BorderThickness="0"
Background="Red">

<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="LightGray" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="BorderThickness" Value="0" />
</Style>
</ListView.ItemContainerStyle>

<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Black" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
</GridView.ColumnHeaderContainerStyle>

<GridViewColumn Header="1" Width="40"/>
<GridViewColumn Header="2" Width="40"/>
<GridViewColumn Header="3" Width="40"/>

</GridView>
</ListView.View>

<ListViewItem> 1 </ListViewItem>
<ListViewItem> 2 </ListViewItem>
</ListView>


The only way so far to achieve the look I am going for is to change the background of the listview to the same color as the headers - there has to be a better way, though. Any help is appreciated!










share|improve this question





























    1















    I am having trouble with the layout of a ListView / GridView combination in my WPF project. The columnheaders are just not spanning the whole width of the parent ListView. I tried to find documentation everywhere but didn't find any clues about the underlying containers / control templates.



    I guess the standard control template of the header contains something that I am not taking care of, I just cannot wrap my head around it.



    The (simplified) ListView has a few pixel space to the left and to the right of the row containing the headers, where the background of the parent ListView is visible:



    https://i.imgur.com/XijV88a.jpg



    Question: How can I make the columnheaders span the whole width of the ListView and make the few pixels space disappear?



    My XAML:



    <ListView Padding="0"
    BorderThickness="0"
    Background="Red">

    <ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="LightGray" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="BorderThickness" Value="0" />
    </Style>
    </ListView.ItemContainerStyle>

    <ListView.View>
    <GridView>
    <GridView.ColumnHeaderContainerStyle>
    <Style TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background" Value="Black" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    </Style>
    </GridView.ColumnHeaderContainerStyle>

    <GridViewColumn Header="1" Width="40"/>
    <GridViewColumn Header="2" Width="40"/>
    <GridViewColumn Header="3" Width="40"/>

    </GridView>
    </ListView.View>

    <ListViewItem> 1 </ListViewItem>
    <ListViewItem> 2 </ListViewItem>
    </ListView>


    The only way so far to achieve the look I am going for is to change the background of the listview to the same color as the headers - there has to be a better way, though. Any help is appreciated!










    share|improve this question



























      1












      1








      1








      I am having trouble with the layout of a ListView / GridView combination in my WPF project. The columnheaders are just not spanning the whole width of the parent ListView. I tried to find documentation everywhere but didn't find any clues about the underlying containers / control templates.



      I guess the standard control template of the header contains something that I am not taking care of, I just cannot wrap my head around it.



      The (simplified) ListView has a few pixel space to the left and to the right of the row containing the headers, where the background of the parent ListView is visible:



      https://i.imgur.com/XijV88a.jpg



      Question: How can I make the columnheaders span the whole width of the ListView and make the few pixels space disappear?



      My XAML:



      <ListView Padding="0"
      BorderThickness="0"
      Background="Red">

      <ListView.ItemContainerStyle>
      <Style TargetType="{x:Type ListViewItem}">
      <Setter Property="Background" Value="LightGray" />
      <Setter Property="Margin" Value="0" />
      <Setter Property="Padding" Value="0" />
      <Setter Property="BorderThickness" Value="0" />
      </Style>
      </ListView.ItemContainerStyle>

      <ListView.View>
      <GridView>
      <GridView.ColumnHeaderContainerStyle>
      <Style TargetType="{x:Type GridViewColumnHeader}">
      <Setter Property="Foreground" Value="White" />
      <Setter Property="Background" Value="Black" />
      <Setter Property="BorderThickness" Value="0" />
      <Setter Property="Margin" Value="0" />
      <Setter Property="Padding" Value="0" />
      <Setter Property="HorizontalAlignment" Value="Stretch" />
      </Style>
      </GridView.ColumnHeaderContainerStyle>

      <GridViewColumn Header="1" Width="40"/>
      <GridViewColumn Header="2" Width="40"/>
      <GridViewColumn Header="3" Width="40"/>

      </GridView>
      </ListView.View>

      <ListViewItem> 1 </ListViewItem>
      <ListViewItem> 2 </ListViewItem>
      </ListView>


      The only way so far to achieve the look I am going for is to change the background of the listview to the same color as the headers - there has to be a better way, though. Any help is appreciated!










      share|improve this question
















      I am having trouble with the layout of a ListView / GridView combination in my WPF project. The columnheaders are just not spanning the whole width of the parent ListView. I tried to find documentation everywhere but didn't find any clues about the underlying containers / control templates.



      I guess the standard control template of the header contains something that I am not taking care of, I just cannot wrap my head around it.



      The (simplified) ListView has a few pixel space to the left and to the right of the row containing the headers, where the background of the parent ListView is visible:



      https://i.imgur.com/XijV88a.jpg



      Question: How can I make the columnheaders span the whole width of the ListView and make the few pixels space disappear?



      My XAML:



      <ListView Padding="0"
      BorderThickness="0"
      Background="Red">

      <ListView.ItemContainerStyle>
      <Style TargetType="{x:Type ListViewItem}">
      <Setter Property="Background" Value="LightGray" />
      <Setter Property="Margin" Value="0" />
      <Setter Property="Padding" Value="0" />
      <Setter Property="BorderThickness" Value="0" />
      </Style>
      </ListView.ItemContainerStyle>

      <ListView.View>
      <GridView>
      <GridView.ColumnHeaderContainerStyle>
      <Style TargetType="{x:Type GridViewColumnHeader}">
      <Setter Property="Foreground" Value="White" />
      <Setter Property="Background" Value="Black" />
      <Setter Property="BorderThickness" Value="0" />
      <Setter Property="Margin" Value="0" />
      <Setter Property="Padding" Value="0" />
      <Setter Property="HorizontalAlignment" Value="Stretch" />
      </Style>
      </GridView.ColumnHeaderContainerStyle>

      <GridViewColumn Header="1" Width="40"/>
      <GridViewColumn Header="2" Width="40"/>
      <GridViewColumn Header="3" Width="40"/>

      </GridView>
      </ListView.View>

      <ListViewItem> 1 </ListViewItem>
      <ListViewItem> 2 </ListViewItem>
      </ListView>


      The only way so far to achieve the look I am going for is to change the background of the listview to the same color as the headers - there has to be a better way, though. Any help is appreciated!







      wpf xaml listview gridview columnheader






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 9:43







      Krombir

















      asked Nov 15 '18 at 8:47









      KrombirKrombir

      83




      83
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This column does not support "*" as width argument so you cannot easily say - fill the rest...



          What you should do is hook up on SizeChanged event of the ListView and there do the calculations. So for instance, in your case, if we wanted first two columns to take 40 each and the third column to take the rest you would do it like so:



          private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
          {
          ListView listView = sender as ListView;
          GridView gView = listView.View as GridView;

          var workingWidth = listView.ActualWidth - SystemParameters.VerticalScrollBarWidth;

          gView.Columns[0].Width = 40;
          gView.Columns[1].Width = 40;
          gView.Columns[2].Width = workingWidth - 80;
          }


          EDIT



          For the gaps(to the left and to the right) in the header row you want to alter the ColumnHeaderContainerStyle like so:



          <GridView.ColumnHeaderContainerStyle>
          <Style TargetType="{x:Type GridViewColumnHeader}">
          <Setter Property="Foreground" Value="White" />
          <Setter Property="Background" Value="Black" />
          <Setter Property="BorderThickness" Value="0" />
          <Setter Property="Margin" Value="-2,0,-2,0" />
          <Setter Property="Padding" Value="0" />
          <Setter Property="HorizontalAlignment" Value="Stretch" />
          </Style>
          </GridView.ColumnHeaderContainerStyle>


          So make sure you just reduce the margin by -2 on each side.






          share|improve this answer


























          • Thanks for your answer, but this is not what I meant. I am concerned about the spaces to the far right and far left of the header row. There are some pixels that just don't get filled and I don't know why. They are visible in the image, where the red background of the ListView "shines through".

            – Krombir
            Nov 15 '18 at 9:41











          • Thanks a lot, this did the trick! Not sure where those margins come from, but now it looks as desired.

            – Krombir
            Nov 15 '18 at 13:26











          • It would be worthwhile to snoop out the visual tree. I suspect that the header row is in some kind of border that has padding of 2,0,2,0.

            – Rob
            Nov 15 '18 at 13:28











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315475%2fgridviewcolumnheader-not-spanning-whole-listview-width%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          This column does not support "*" as width argument so you cannot easily say - fill the rest...



          What you should do is hook up on SizeChanged event of the ListView and there do the calculations. So for instance, in your case, if we wanted first two columns to take 40 each and the third column to take the rest you would do it like so:



          private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
          {
          ListView listView = sender as ListView;
          GridView gView = listView.View as GridView;

          var workingWidth = listView.ActualWidth - SystemParameters.VerticalScrollBarWidth;

          gView.Columns[0].Width = 40;
          gView.Columns[1].Width = 40;
          gView.Columns[2].Width = workingWidth - 80;
          }


          EDIT



          For the gaps(to the left and to the right) in the header row you want to alter the ColumnHeaderContainerStyle like so:



          <GridView.ColumnHeaderContainerStyle>
          <Style TargetType="{x:Type GridViewColumnHeader}">
          <Setter Property="Foreground" Value="White" />
          <Setter Property="Background" Value="Black" />
          <Setter Property="BorderThickness" Value="0" />
          <Setter Property="Margin" Value="-2,0,-2,0" />
          <Setter Property="Padding" Value="0" />
          <Setter Property="HorizontalAlignment" Value="Stretch" />
          </Style>
          </GridView.ColumnHeaderContainerStyle>


          So make sure you just reduce the margin by -2 on each side.






          share|improve this answer


























          • Thanks for your answer, but this is not what I meant. I am concerned about the spaces to the far right and far left of the header row. There are some pixels that just don't get filled and I don't know why. They are visible in the image, where the red background of the ListView "shines through".

            – Krombir
            Nov 15 '18 at 9:41











          • Thanks a lot, this did the trick! Not sure where those margins come from, but now it looks as desired.

            – Krombir
            Nov 15 '18 at 13:26











          • It would be worthwhile to snoop out the visual tree. I suspect that the header row is in some kind of border that has padding of 2,0,2,0.

            – Rob
            Nov 15 '18 at 13:28
















          0














          This column does not support "*" as width argument so you cannot easily say - fill the rest...



          What you should do is hook up on SizeChanged event of the ListView and there do the calculations. So for instance, in your case, if we wanted first two columns to take 40 each and the third column to take the rest you would do it like so:



          private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
          {
          ListView listView = sender as ListView;
          GridView gView = listView.View as GridView;

          var workingWidth = listView.ActualWidth - SystemParameters.VerticalScrollBarWidth;

          gView.Columns[0].Width = 40;
          gView.Columns[1].Width = 40;
          gView.Columns[2].Width = workingWidth - 80;
          }


          EDIT



          For the gaps(to the left and to the right) in the header row you want to alter the ColumnHeaderContainerStyle like so:



          <GridView.ColumnHeaderContainerStyle>
          <Style TargetType="{x:Type GridViewColumnHeader}">
          <Setter Property="Foreground" Value="White" />
          <Setter Property="Background" Value="Black" />
          <Setter Property="BorderThickness" Value="0" />
          <Setter Property="Margin" Value="-2,0,-2,0" />
          <Setter Property="Padding" Value="0" />
          <Setter Property="HorizontalAlignment" Value="Stretch" />
          </Style>
          </GridView.ColumnHeaderContainerStyle>


          So make sure you just reduce the margin by -2 on each side.






          share|improve this answer


























          • Thanks for your answer, but this is not what I meant. I am concerned about the spaces to the far right and far left of the header row. There are some pixels that just don't get filled and I don't know why. They are visible in the image, where the red background of the ListView "shines through".

            – Krombir
            Nov 15 '18 at 9:41











          • Thanks a lot, this did the trick! Not sure where those margins come from, but now it looks as desired.

            – Krombir
            Nov 15 '18 at 13:26











          • It would be worthwhile to snoop out the visual tree. I suspect that the header row is in some kind of border that has padding of 2,0,2,0.

            – Rob
            Nov 15 '18 at 13:28














          0












          0








          0







          This column does not support "*" as width argument so you cannot easily say - fill the rest...



          What you should do is hook up on SizeChanged event of the ListView and there do the calculations. So for instance, in your case, if we wanted first two columns to take 40 each and the third column to take the rest you would do it like so:



          private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
          {
          ListView listView = sender as ListView;
          GridView gView = listView.View as GridView;

          var workingWidth = listView.ActualWidth - SystemParameters.VerticalScrollBarWidth;

          gView.Columns[0].Width = 40;
          gView.Columns[1].Width = 40;
          gView.Columns[2].Width = workingWidth - 80;
          }


          EDIT



          For the gaps(to the left and to the right) in the header row you want to alter the ColumnHeaderContainerStyle like so:



          <GridView.ColumnHeaderContainerStyle>
          <Style TargetType="{x:Type GridViewColumnHeader}">
          <Setter Property="Foreground" Value="White" />
          <Setter Property="Background" Value="Black" />
          <Setter Property="BorderThickness" Value="0" />
          <Setter Property="Margin" Value="-2,0,-2,0" />
          <Setter Property="Padding" Value="0" />
          <Setter Property="HorizontalAlignment" Value="Stretch" />
          </Style>
          </GridView.ColumnHeaderContainerStyle>


          So make sure you just reduce the margin by -2 on each side.






          share|improve this answer















          This column does not support "*" as width argument so you cannot easily say - fill the rest...



          What you should do is hook up on SizeChanged event of the ListView and there do the calculations. So for instance, in your case, if we wanted first two columns to take 40 each and the third column to take the rest you would do it like so:



          private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
          {
          ListView listView = sender as ListView;
          GridView gView = listView.View as GridView;

          var workingWidth = listView.ActualWidth - SystemParameters.VerticalScrollBarWidth;

          gView.Columns[0].Width = 40;
          gView.Columns[1].Width = 40;
          gView.Columns[2].Width = workingWidth - 80;
          }


          EDIT



          For the gaps(to the left and to the right) in the header row you want to alter the ColumnHeaderContainerStyle like so:



          <GridView.ColumnHeaderContainerStyle>
          <Style TargetType="{x:Type GridViewColumnHeader}">
          <Setter Property="Foreground" Value="White" />
          <Setter Property="Background" Value="Black" />
          <Setter Property="BorderThickness" Value="0" />
          <Setter Property="Margin" Value="-2,0,-2,0" />
          <Setter Property="Padding" Value="0" />
          <Setter Property="HorizontalAlignment" Value="Stretch" />
          </Style>
          </GridView.ColumnHeaderContainerStyle>


          So make sure you just reduce the margin by -2 on each side.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 13:18

























          answered Nov 15 '18 at 9:31









          RobRob

          1,0421123




          1,0421123













          • Thanks for your answer, but this is not what I meant. I am concerned about the spaces to the far right and far left of the header row. There are some pixels that just don't get filled and I don't know why. They are visible in the image, where the red background of the ListView "shines through".

            – Krombir
            Nov 15 '18 at 9:41











          • Thanks a lot, this did the trick! Not sure where those margins come from, but now it looks as desired.

            – Krombir
            Nov 15 '18 at 13:26











          • It would be worthwhile to snoop out the visual tree. I suspect that the header row is in some kind of border that has padding of 2,0,2,0.

            – Rob
            Nov 15 '18 at 13:28



















          • Thanks for your answer, but this is not what I meant. I am concerned about the spaces to the far right and far left of the header row. There are some pixels that just don't get filled and I don't know why. They are visible in the image, where the red background of the ListView "shines through".

            – Krombir
            Nov 15 '18 at 9:41











          • Thanks a lot, this did the trick! Not sure where those margins come from, but now it looks as desired.

            – Krombir
            Nov 15 '18 at 13:26











          • It would be worthwhile to snoop out the visual tree. I suspect that the header row is in some kind of border that has padding of 2,0,2,0.

            – Rob
            Nov 15 '18 at 13:28

















          Thanks for your answer, but this is not what I meant. I am concerned about the spaces to the far right and far left of the header row. There are some pixels that just don't get filled and I don't know why. They are visible in the image, where the red background of the ListView "shines through".

          – Krombir
          Nov 15 '18 at 9:41





          Thanks for your answer, but this is not what I meant. I am concerned about the spaces to the far right and far left of the header row. There are some pixels that just don't get filled and I don't know why. They are visible in the image, where the red background of the ListView "shines through".

          – Krombir
          Nov 15 '18 at 9:41













          Thanks a lot, this did the trick! Not sure where those margins come from, but now it looks as desired.

          – Krombir
          Nov 15 '18 at 13:26





          Thanks a lot, this did the trick! Not sure where those margins come from, but now it looks as desired.

          – Krombir
          Nov 15 '18 at 13:26













          It would be worthwhile to snoop out the visual tree. I suspect that the header row is in some kind of border that has padding of 2,0,2,0.

          – Rob
          Nov 15 '18 at 13:28





          It would be worthwhile to snoop out the visual tree. I suspect that the header row is in some kind of border that has padding of 2,0,2,0.

          – Rob
          Nov 15 '18 at 13:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315475%2fgridviewcolumnheader-not-spanning-whole-listview-width%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine