ListView所有列的宽度相等

时间:2013-05-03 06:19:54

标签: wpf listview

在加载到GUI中时,如何为listview中的所有列设置相等的宽度?不在XAML中。

谢谢, NAG

2 个答案:

答案 0 :(得分:0)

你可以使用它,但它不是一个很好的解决方案..

    ListView listview = new ListView();
    GridView gridview = new GridView();

    GridViewColumn gridviewcolumn1 = new GridViewColumn();
    gridviewcolumn1.Width = 10;
    GridViewColumn gridviewcolumn2 = new GridViewColumn();
    gridviewcolumn2.Width = 10;
    GridViewColumn gridviewcolumn3 = new GridViewColumn();
    gridviewcolumn3.Width = 10;


    gridview.Columns.Add(gridviewcolumn1);
    gridview.Columns.Add(gridviewcolumn2);
    gridview.Columns.Add(gridviewcolumn3);

    listview.View = gridview;

答案 1 :(得分:0)

尝试SizeChanged上的ListView事件。

<强> XAML

<ListView x:Name='ListView1' 
         HorizontalAlignment='Stretch'
         Margin='12'
         VerticalAlignment='Stretch'>
  <ListView.View>
    <GridView x:Name='Gridview1'>
       <GridViewColumn Header='First' />
     <GridViewColumn Header='Second' />
       <GridViewColumn Header='Third' />
   </GridView>
</ListView.View>

<强> C#

 public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      ListView1.SizeChanged += LV_SizeChanged;


    }

    void LV_SizeChanged(object sender, SizeChangedEventArgs e)
    {
      var newWidth = ListView1.ActualWidth / Gridview1.Columns.Count;
      foreach (var column in Gridview1.Columns)
      {
        column.Width = newWidth;
      }
    }
  }