1

I'm making an WPF-application that has a databound ListView. When loading the application, the width of the ListViewColumns will auto-resize, but when adding or changing an item it doesn't auto-resize. I've tried refreshing the listview, setting the width of the column to auto, -1 or -2 in both xaml and VB-code and I've tries changing the itemssource to nothing before refilling it with items. This is the xaml code:

<ListView x:Name="lsvPersons" Margin="5,5,5,35" ItemsSource="{Binding Persons}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
            <GridViewColumn DisplayMemberBinding="{Binding Gender}" Header="Gender"/>
            <GridViewColumn DisplayMemberBinding="{Binding Age}" Header="Age"/>
        </GridView>
    </ListView.View>
</ListView>
<Button x:Name="btnAddPerson" Content="Add" Height="25" Margin="0,0,200,5" Width="80"/>

The binding works with a controller, which gets the persons with Person.getPersons from a SQL-database:

Private oController As New MainController()
Public Sub New()
    MyBase.New()
    Me.InitializeComponent()
    Me.DataContext = oController
End Sub

After pressing the button, a window will open to add a person. After the window is closed, the item is added to the listview with following code, which refreshes the items in the listview:

lsvPersons.ItemsSource = Person.getPersons()

So, what do I need to do to auto-resize the columns of the listview when an item is added or editted?

Jona
  • 181
  • 2
  • 9

1 Answers1

0
        GridView gv = lvSrchResulsGrid.View as GridView;
        if (gv != null)
        {
            int colNum = 0;
            foreach (GridViewColumn c in gv.Columns)
            {
                // Code below was found in GridViewColumnHeader.OnGripperDoubleClicked() event handler (using Reflector)
                // i.e. it is the same code that is executed when the gripper is double clicked
                // if (adjustAllColumns || App.StaticGabeLib.FieldDefsGrid[colNum].DispGrid)
                // if (adjustAllColumns || fdGridSorted[colNum].AppliedDispGrid)
                // {
                    if (double.IsNaN(c.Width))
                    {
                        c.Width = c.ActualWidth;
                    }
                    c.Width = double.NaN;
                // }
            }
        }
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • I've tried this code without the `if (adjustAllColumns || fdGridSorted[colNum].AppliedDispGrid)` part, but it doesn't work for me. Is this if-statement necessary? If yes, is there as Visual Basic equivalent of it? – Jona Dec 04 '11 at 21:17
  • Sprry that was a copy of my working code. Just take the if out. – paparazzo Dec 05 '11 at 15:44
  • The code without the if doesn't seem to do anything to the listview. Since I'm using a custom WPF Theme, could this be the cause it isn't working? (Tested it by using the default theme for a listview, still didn't work) – Jona Dec 05 '11 at 19:16
  • Sorry man, that is from working production code. Something must be diffenent in my environment. – paparazzo Dec 06 '11 at 20:28