WPF ListView 自动调整列宽

ListView 控件:
 
            
                
                    
                        
                            
                                
                            
                        
                    
                     
                        
                            
                                
                            
                        
                    
                
            
 

在 GridViewColumn 中设置

Width="*"
或者是在 DataTemplate 中 设置
Width="auto"

并不能达到自动 , 列宽自动调整的效果, 列宽在控件第一次加载的时候已经确定,之后不会随着 某列数据长度的增加和减少 而改变列宽。


找了很多方法,感觉下面的这中方法比较简单和实用,在每次增加或者删除数据集合的时候,添加下列代码:

//调整列宽
            GridView gv = lvTrans.View as GridView;
            if (gv != null)
            {
                foreach (GridViewColumn gvc in gv.Columns)
                {
                    gvc.Width = gvc.ActualWidth;
                    gvc.Width = Double.NaN;
                }
            }
将 GridViewColumn 的 Width 属性 设置为 无效值,触发其自适应效果。


你可能感兴趣的:(WPF)