Wpf 添加项目时调整数据绑定ListViewColumns的大小
我正在制作一个具有数据绑定ListView的WPF应用程序。加载应用程序时,ListViewColumns的宽度将自动调整大小,但添加或更改项目时不会自动调整大小。我尝试刷新listview,在xaml和VB代码中将列的宽度设置为auto,-1或-2,并尝试在使用项重新填充之前将itemssource更改为nothing。这是xaml代码:Wpf 添加项目时调整数据绑定ListViewColumns的大小,wpf,vb.net,listview,autoresize,databound,Wpf,Vb.net,Listview,Autoresize,Databound,我正在制作一个具有数据绑定ListView的WPF应用程序。加载应用程序时,ListViewColumns的宽度将自动调整大小,但添加或更改项目时不会自动调整大小。我尝试刷新listview,在xaml和VB代码中将列的宽度设置为auto,-1或-2,并尝试在使用项重新填充之前将itemssource更改为nothing。这是xaml代码: <ListView x:Name="lsvPersons" Margin="5,5,5,35" ItemsSource="{Binding Perso
<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"/>
按下按钮后,将打开一个窗口添加人员。关闭窗口后,将使用以下代码将该项添加到listview,刷新listview中的项:
lsvPersons.ItemsSource = Person.getPersons()
那么,当添加或编辑一个项目时,我需要做什么来自动调整listview列的大小呢?我尝试了这段代码,但没有使用if(adjustAllColumns | | fdGridSorted[colNum].AppliedDispGrid)部分,但它对我不起作用。这是否有必要?如果是,是否有与之相当的Visual Basic?Sprry那是我工作代码的副本。只需去掉if。没有if的代码似乎对listview没有任何作用。因为我使用的是自定义WPF主题,这可能是它无法工作的原因吗?(通过使用listview的默认主题进行了测试,但仍然不起作用)对不起,这是生产代码。在我的环境中一定有不同的东西。
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;
// }
}
}
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;
// }
}
}