当列表视图加载到 GUI 时,我们如何为列表视图中的所有列设置相等的宽度?不在 XAML 中。
谢谢,唠叨
Try the SizeChanged
event on the 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;
}
}
}
您可以使用它,但它不是一个好的解决方案..
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;