GridView dynamic column width in WPF












4















<Window x:Class="GridViewSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListView Name="CheckoutList" Margin="0,0,8,0">
<ListView.View>
<GridView x:Name="CheckOutGridView">
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink >
<TextBlock Text="{Binding Path=Text}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink>
<TextBlock Text="Remove"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

<StackPanel Grid.Row="1">
<Button Width="100" Height="25" Click="Button_Click" >name</Button>
<Button Width="100" Height="25" Click="Button_Click1" >long name</Button>
</StackPanel>
</Grid>




public partial class Window1 : Window
{
class TextObject
{
private string _text;

public TextObject(string Text)
{
_text = Text;
}

public string Text
{
get { return _text; }
set { _text = value; }
}
}


public Window1()
{
InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
CheckoutList.Items.Add(new TextObject("name"));
}

private void Button_Click1(object sender, RoutedEventArgs e)
{
CheckoutList.Items.Add(new TextObject("long name"));
}

}


Given the above sample press name then long name. How come the column doesn't resize to fit its contents?










share|improve this question



























    4















    <Window x:Class="GridViewSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" >
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
    </Grid.RowDefinitions>
    <ListView Name="CheckoutList" Margin="0,0,8,0">
    <ListView.View>
    <GridView x:Name="CheckOutGridView">
    <GridView.ColumnHeaderContainerStyle>
    <Style TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Visibility" Value="Collapsed"/>
    </Style>
    </GridView.ColumnHeaderContainerStyle>
    <GridViewColumn>
    <GridViewColumn.CellTemplate>
    <DataTemplate>
    <TextBlock>
    <Hyperlink >
    <TextBlock Text="{Binding Path=Text}"/>
    </Hyperlink>
    </TextBlock>
    </DataTemplate>
    </GridViewColumn.CellTemplate>
    </GridViewColumn>
    <GridViewColumn>
    <GridViewColumn.CellTemplate>
    <DataTemplate>
    <TextBlock>
    <Hyperlink>
    <TextBlock Text="Remove"/>
    </Hyperlink>
    </TextBlock>
    </DataTemplate>
    </GridViewColumn.CellTemplate>
    </GridViewColumn>
    </GridView>
    </ListView.View>
    </ListView>

    <StackPanel Grid.Row="1">
    <Button Width="100" Height="25" Click="Button_Click" >name</Button>
    <Button Width="100" Height="25" Click="Button_Click1" >long name</Button>
    </StackPanel>
    </Grid>




    public partial class Window1 : Window
    {
    class TextObject
    {
    private string _text;

    public TextObject(string Text)
    {
    _text = Text;
    }

    public string Text
    {
    get { return _text; }
    set { _text = value; }
    }
    }


    public Window1()
    {
    InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
    CheckoutList.Items.Add(new TextObject("name"));
    }

    private void Button_Click1(object sender, RoutedEventArgs e)
    {
    CheckoutList.Items.Add(new TextObject("long name"));
    }

    }


    Given the above sample press name then long name. How come the column doesn't resize to fit its contents?










    share|improve this question

























      4












      4








      4


      0






      <Window x:Class="GridViewSample.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" >
      <Grid>
      <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition/>
      </Grid.RowDefinitions>
      <ListView Name="CheckoutList" Margin="0,0,8,0">
      <ListView.View>
      <GridView x:Name="CheckOutGridView">
      <GridView.ColumnHeaderContainerStyle>
      <Style TargetType="{x:Type GridViewColumnHeader}">
      <Setter Property="Visibility" Value="Collapsed"/>
      </Style>
      </GridView.ColumnHeaderContainerStyle>
      <GridViewColumn>
      <GridViewColumn.CellTemplate>
      <DataTemplate>
      <TextBlock>
      <Hyperlink >
      <TextBlock Text="{Binding Path=Text}"/>
      </Hyperlink>
      </TextBlock>
      </DataTemplate>
      </GridViewColumn.CellTemplate>
      </GridViewColumn>
      <GridViewColumn>
      <GridViewColumn.CellTemplate>
      <DataTemplate>
      <TextBlock>
      <Hyperlink>
      <TextBlock Text="Remove"/>
      </Hyperlink>
      </TextBlock>
      </DataTemplate>
      </GridViewColumn.CellTemplate>
      </GridViewColumn>
      </GridView>
      </ListView.View>
      </ListView>

      <StackPanel Grid.Row="1">
      <Button Width="100" Height="25" Click="Button_Click" >name</Button>
      <Button Width="100" Height="25" Click="Button_Click1" >long name</Button>
      </StackPanel>
      </Grid>




      public partial class Window1 : Window
      {
      class TextObject
      {
      private string _text;

      public TextObject(string Text)
      {
      _text = Text;
      }

      public string Text
      {
      get { return _text; }
      set { _text = value; }
      }
      }


      public Window1()
      {
      InitializeComponent();
      }

      private void Button_Click(object sender, RoutedEventArgs e)
      {
      CheckoutList.Items.Add(new TextObject("name"));
      }

      private void Button_Click1(object sender, RoutedEventArgs e)
      {
      CheckoutList.Items.Add(new TextObject("long name"));
      }

      }


      Given the above sample press name then long name. How come the column doesn't resize to fit its contents?










      share|improve this question














      <Window x:Class="GridViewSample.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" >
      <Grid>
      <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition/>
      </Grid.RowDefinitions>
      <ListView Name="CheckoutList" Margin="0,0,8,0">
      <ListView.View>
      <GridView x:Name="CheckOutGridView">
      <GridView.ColumnHeaderContainerStyle>
      <Style TargetType="{x:Type GridViewColumnHeader}">
      <Setter Property="Visibility" Value="Collapsed"/>
      </Style>
      </GridView.ColumnHeaderContainerStyle>
      <GridViewColumn>
      <GridViewColumn.CellTemplate>
      <DataTemplate>
      <TextBlock>
      <Hyperlink >
      <TextBlock Text="{Binding Path=Text}"/>
      </Hyperlink>
      </TextBlock>
      </DataTemplate>
      </GridViewColumn.CellTemplate>
      </GridViewColumn>
      <GridViewColumn>
      <GridViewColumn.CellTemplate>
      <DataTemplate>
      <TextBlock>
      <Hyperlink>
      <TextBlock Text="Remove"/>
      </Hyperlink>
      </TextBlock>
      </DataTemplate>
      </GridViewColumn.CellTemplate>
      </GridViewColumn>
      </GridView>
      </ListView.View>
      </ListView>

      <StackPanel Grid.Row="1">
      <Button Width="100" Height="25" Click="Button_Click" >name</Button>
      <Button Width="100" Height="25" Click="Button_Click1" >long name</Button>
      </StackPanel>
      </Grid>




      public partial class Window1 : Window
      {
      class TextObject
      {
      private string _text;

      public TextObject(string Text)
      {
      _text = Text;
      }

      public string Text
      {
      get { return _text; }
      set { _text = value; }
      }
      }


      public Window1()
      {
      InitializeComponent();
      }

      private void Button_Click(object sender, RoutedEventArgs e)
      {
      CheckoutList.Items.Add(new TextObject("name"));
      }

      private void Button_Click1(object sender, RoutedEventArgs e)
      {
      CheckoutList.Items.Add(new TextObject("long name"));
      }

      }


      Given the above sample press name then long name. How come the column doesn't resize to fit its contents?







      wpf gridview listview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 30 '09 at 23:17









      TheNameisGuyTheNameisGuy

      2112




      2112
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Try



          <GridViewColumn Width="{x:Static System:Double.NaN}">


          where



          xmlns:System="clr-namespace:System;assembly=mscorlib"


          and this should help you resize the column to fit the header content.






          share|improve this answer
























          • It works, because setting the column width to NaN allows that column to measure to infinity, thus, filling up all the space of its parent container.

            – Tri Q Tran
            Feb 23 '12 at 21:41











          • But isnt there a Double.PositiveInfinity? Is this behavior programmed in (as in somewhere they check Double.IsNan(width) and set it to Auto in the code of the GridViewColumn) or is it due to the nature of the Double.Nan itself?

            – Jason Ridge
            Feb 24 '12 at 5:50








          • 1





            This is not the nature of Double.NaN but the nature of WPF to measure its available width vs. container width. By setting column width to Double.NaN will 'fool' WPF by 'not setting a fixed width' and this creates an illusion of 'Auto' width.

            – Tri Q Tran
            Feb 27 '12 at 4:51











          • Isn't this the same as Width="Auto" - at least, it is for me, as I get the same results.

            – imekon
            Jul 18 '12 at 10:33











          • @imekon As far as I know Width="Auto" has performance issue as it should wait for all rows to be loaded so it can define its Width. it's really not recommended on data columns.

            – HichemSeeSharp
            Jun 11 '13 at 21:12













          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%2f1501161%2fgridview-dynamic-column-width-in-wpf%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














          Try



          <GridViewColumn Width="{x:Static System:Double.NaN}">


          where



          xmlns:System="clr-namespace:System;assembly=mscorlib"


          and this should help you resize the column to fit the header content.






          share|improve this answer
























          • It works, because setting the column width to NaN allows that column to measure to infinity, thus, filling up all the space of its parent container.

            – Tri Q Tran
            Feb 23 '12 at 21:41











          • But isnt there a Double.PositiveInfinity? Is this behavior programmed in (as in somewhere they check Double.IsNan(width) and set it to Auto in the code of the GridViewColumn) or is it due to the nature of the Double.Nan itself?

            – Jason Ridge
            Feb 24 '12 at 5:50








          • 1





            This is not the nature of Double.NaN but the nature of WPF to measure its available width vs. container width. By setting column width to Double.NaN will 'fool' WPF by 'not setting a fixed width' and this creates an illusion of 'Auto' width.

            – Tri Q Tran
            Feb 27 '12 at 4:51











          • Isn't this the same as Width="Auto" - at least, it is for me, as I get the same results.

            – imekon
            Jul 18 '12 at 10:33











          • @imekon As far as I know Width="Auto" has performance issue as it should wait for all rows to be loaded so it can define its Width. it's really not recommended on data columns.

            – HichemSeeSharp
            Jun 11 '13 at 21:12


















          0














          Try



          <GridViewColumn Width="{x:Static System:Double.NaN}">


          where



          xmlns:System="clr-namespace:System;assembly=mscorlib"


          and this should help you resize the column to fit the header content.






          share|improve this answer
























          • It works, because setting the column width to NaN allows that column to measure to infinity, thus, filling up all the space of its parent container.

            – Tri Q Tran
            Feb 23 '12 at 21:41











          • But isnt there a Double.PositiveInfinity? Is this behavior programmed in (as in somewhere they check Double.IsNan(width) and set it to Auto in the code of the GridViewColumn) or is it due to the nature of the Double.Nan itself?

            – Jason Ridge
            Feb 24 '12 at 5:50








          • 1





            This is not the nature of Double.NaN but the nature of WPF to measure its available width vs. container width. By setting column width to Double.NaN will 'fool' WPF by 'not setting a fixed width' and this creates an illusion of 'Auto' width.

            – Tri Q Tran
            Feb 27 '12 at 4:51











          • Isn't this the same as Width="Auto" - at least, it is for me, as I get the same results.

            – imekon
            Jul 18 '12 at 10:33











          • @imekon As far as I know Width="Auto" has performance issue as it should wait for all rows to be loaded so it can define its Width. it's really not recommended on data columns.

            – HichemSeeSharp
            Jun 11 '13 at 21:12
















          0












          0








          0







          Try



          <GridViewColumn Width="{x:Static System:Double.NaN}">


          where



          xmlns:System="clr-namespace:System;assembly=mscorlib"


          and this should help you resize the column to fit the header content.






          share|improve this answer













          Try



          <GridViewColumn Width="{x:Static System:Double.NaN}">


          where



          xmlns:System="clr-namespace:System;assembly=mscorlib"


          and this should help you resize the column to fit the header content.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 14 '09 at 14:59









          Tri Q TranTri Q Tran

          4,08112754




          4,08112754













          • It works, because setting the column width to NaN allows that column to measure to infinity, thus, filling up all the space of its parent container.

            – Tri Q Tran
            Feb 23 '12 at 21:41











          • But isnt there a Double.PositiveInfinity? Is this behavior programmed in (as in somewhere they check Double.IsNan(width) and set it to Auto in the code of the GridViewColumn) or is it due to the nature of the Double.Nan itself?

            – Jason Ridge
            Feb 24 '12 at 5:50








          • 1





            This is not the nature of Double.NaN but the nature of WPF to measure its available width vs. container width. By setting column width to Double.NaN will 'fool' WPF by 'not setting a fixed width' and this creates an illusion of 'Auto' width.

            – Tri Q Tran
            Feb 27 '12 at 4:51











          • Isn't this the same as Width="Auto" - at least, it is for me, as I get the same results.

            – imekon
            Jul 18 '12 at 10:33











          • @imekon As far as I know Width="Auto" has performance issue as it should wait for all rows to be loaded so it can define its Width. it's really not recommended on data columns.

            – HichemSeeSharp
            Jun 11 '13 at 21:12





















          • It works, because setting the column width to NaN allows that column to measure to infinity, thus, filling up all the space of its parent container.

            – Tri Q Tran
            Feb 23 '12 at 21:41











          • But isnt there a Double.PositiveInfinity? Is this behavior programmed in (as in somewhere they check Double.IsNan(width) and set it to Auto in the code of the GridViewColumn) or is it due to the nature of the Double.Nan itself?

            – Jason Ridge
            Feb 24 '12 at 5:50








          • 1





            This is not the nature of Double.NaN but the nature of WPF to measure its available width vs. container width. By setting column width to Double.NaN will 'fool' WPF by 'not setting a fixed width' and this creates an illusion of 'Auto' width.

            – Tri Q Tran
            Feb 27 '12 at 4:51











          • Isn't this the same as Width="Auto" - at least, it is for me, as I get the same results.

            – imekon
            Jul 18 '12 at 10:33











          • @imekon As far as I know Width="Auto" has performance issue as it should wait for all rows to be loaded so it can define its Width. it's really not recommended on data columns.

            – HichemSeeSharp
            Jun 11 '13 at 21:12



















          It works, because setting the column width to NaN allows that column to measure to infinity, thus, filling up all the space of its parent container.

          – Tri Q Tran
          Feb 23 '12 at 21:41





          It works, because setting the column width to NaN allows that column to measure to infinity, thus, filling up all the space of its parent container.

          – Tri Q Tran
          Feb 23 '12 at 21:41













          But isnt there a Double.PositiveInfinity? Is this behavior programmed in (as in somewhere they check Double.IsNan(width) and set it to Auto in the code of the GridViewColumn) or is it due to the nature of the Double.Nan itself?

          – Jason Ridge
          Feb 24 '12 at 5:50







          But isnt there a Double.PositiveInfinity? Is this behavior programmed in (as in somewhere they check Double.IsNan(width) and set it to Auto in the code of the GridViewColumn) or is it due to the nature of the Double.Nan itself?

          – Jason Ridge
          Feb 24 '12 at 5:50






          1




          1





          This is not the nature of Double.NaN but the nature of WPF to measure its available width vs. container width. By setting column width to Double.NaN will 'fool' WPF by 'not setting a fixed width' and this creates an illusion of 'Auto' width.

          – Tri Q Tran
          Feb 27 '12 at 4:51





          This is not the nature of Double.NaN but the nature of WPF to measure its available width vs. container width. By setting column width to Double.NaN will 'fool' WPF by 'not setting a fixed width' and this creates an illusion of 'Auto' width.

          – Tri Q Tran
          Feb 27 '12 at 4:51













          Isn't this the same as Width="Auto" - at least, it is for me, as I get the same results.

          – imekon
          Jul 18 '12 at 10:33





          Isn't this the same as Width="Auto" - at least, it is for me, as I get the same results.

          – imekon
          Jul 18 '12 at 10:33













          @imekon As far as I know Width="Auto" has performance issue as it should wait for all rows to be loaded so it can define its Width. it's really not recommended on data columns.

          – HichemSeeSharp
          Jun 11 '13 at 21:12







          @imekon As far as I know Width="Auto" has performance issue as it should wait for all rows to be loaded so it can define its Width. it's really not recommended on data columns.

          – HichemSeeSharp
          Jun 11 '13 at 21:12






















          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%2f1501161%2fgridview-dynamic-column-width-in-wpf%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma