我有一个wpf控件,里面有很多行,我想在多个页面上打印。我目前正在做的是逐行添加每一行,看看它们是否对于当前页面来说太大了。如果是,我创建一个新版本的新页面控件,我开始添加行。
但是,在我向控件添加一行并调用Measure后,我注意到控件的DesiredSize.Height仍然是相同的。我之前在Silverlight应用程序中使用过这种技术,它可以很好地检测对于页面来说太大的控件。
Size measureSize = new Size(printDlg.PrintableAreaWidth, double.PositiveInfinity);
//tripList is what I want the control to display
while (_rowIndex < tripList.Count)
{
//add child element
controlToPrint.AddRow(tripList[_rowIndex]);
//re-measure
controlToPrint.Measure(measureSize);
//Do we go into the next page?
if (controlToPrint.DesiredSize.Height > printDlg.PrintableAreaHeight)
{
//Create new page
}
++_rowIndex
}
答案 0 :(得分:-1)
猜猜你使用的是ListView:
int StopIndex;
double hgt = 0.0;
for(int i = 0; i< tripList.Count; i++){
controlToPrint.AddRow(tripList[i]);
hgt += controlToPrintf.Items[i].Height; //You didn't mentioned what control you are using, so I guess it's a ListView
if(hgt > controlToPrintf.Height) {
controlToPrint.RemoveRow(tripList[i]); //Remove last ítem that caused rows exced the control's height
StopIndex = --i; //Store the index of the item the control didn't allowed more rows to be added.
break;
}
}
如果手动设置高度,则可以检测控件中的行数:
//Guessing you want a 50 pixel height row:
for(int i=0; i<(int)ControlToPrint.Height/50; i++){ //controlToPrintf.Height/50 rows can be added here
controlToPrint.AddRow(triplist[i]);
controlToPrintf.Items[i].Height = 50;
}