GridView dynamic column width in WPF
<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
add a comment |
<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
add a comment |
<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
<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
wpf gridview listview
asked Sep 30 '09 at 23:17
TheNameisGuyTheNameisGuy
2112
2112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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 aDouble.PositiveInfinity
? Is this behavior programmed in (as in somewhere they checkDouble.IsNan(width)
and set it toAuto
in the code of theGridViewColumn
) or is it due to the nature of theDouble.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 asWidth="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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 aDouble.PositiveInfinity
? Is this behavior programmed in (as in somewhere they checkDouble.IsNan(width)
and set it toAuto
in the code of theGridViewColumn
) or is it due to the nature of theDouble.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 asWidth="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
add a comment |
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.
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 aDouble.PositiveInfinity
? Is this behavior programmed in (as in somewhere they checkDouble.IsNan(width)
and set it toAuto
in the code of theGridViewColumn
) or is it due to the nature of theDouble.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 asWidth="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
add a comment |
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.
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.
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 aDouble.PositiveInfinity
? Is this behavior programmed in (as in somewhere they checkDouble.IsNan(width)
and set it toAuto
in the code of theGridViewColumn
) or is it due to the nature of theDouble.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 asWidth="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
add a comment |
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 aDouble.PositiveInfinity
? Is this behavior programmed in (as in somewhere they checkDouble.IsNan(width)
and set it toAuto
in the code of theGridViewColumn
) or is it due to the nature of theDouble.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 asWidth="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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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