I'm trying to get gridviewcolumns in a listview to auto-size to content on when the Binding source is updated (an observablecollection on a viewmodel).
The listview populates fine, but not working when I refresh/update the collection. This is the solution I'm trying so far:
XAML:
<ListView x:Name="ListView" ItemsSource="{Binding Collection, NotifyOnSourceUpdated=True}" SourceUpdated="ListView_SourceUpdated">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" Header="Test" DisplayMemberBinding={Binding Test}" />
</GridView>
</ListView.View>
</ListView>
Codebehind:
private void requestsListView_SourceUpdated(object sender, DataTransferEventArgs e)
{
GridView gv = requestsListView.View as GridView;
if (gv != null)
{
foreach (var c in gv.Columns)
{
if (double.IsNaN(c.Width))
{
c.Width = c.ActualWidth;
}
c.Width = double.NaN;
}
}
}
From what I can tell, the SourceUpdated event never fires. I don't know if this is because the datacontext is set to a ViewModel? Not sure how to interact with the ListView from the VM.
Is there a better way to try to do this using the ViewModel? Still new and trying to learn MVVM.