Reputation: 59
I wrote code in XAML - WPF Browser Application - Page , just one Listview
and one button
to add new data to the listview
(from other file), I am trying to make the first column auto size itself when the button is pushed, I am using Visual Studio c# 2010.
I've used the following method in the code behind, but AutoResizeColumns
won't be recognized and gives an error.
Unfortunately, none of the previously suggested solutions worked with me.
The Code Behind
public partial class Page1 : Page, INotifyPropertyChanged
{
public Page1()
{
InitializeComponent();
this.DataContext = new Page1Model();
}
private void TestListe_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
public event PropertyChangedEventHandler PropertyChanged;
private void Button1_Click(object sender, RoutedEventArgs e)
{
TestListe1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
}
XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<ListView Name="TestListe1" Margin="68,22,421,8" FontSize="12" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="auto"> <GridViewColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="ST1" Margin="10,0,10,1"/>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding One}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<Button Name="Button1" Grid.Row="1" Height="27" Width="95" Margin="262,24,444,74" Click="Button1_Click" />
</Grid>
Upvotes: 2
Views: 9510
Reputation: 3953
AutoResizeColumns
is from the namespace System.Windows.Forms
. I'm not sure if that will work with WPF or not. But you can set the width of the column to NAN
to make it resize
In your XAML
if you name your GridView as follows:
<GridView x:Name="dataGridView">
Then you could resize all columns with this
foreach (GridViewColumn c in dataGridView.Columns)
{
c.Width = 0; //set it to no width
c.Width = double.NaN; //resize it automatically
}
Upvotes: 9