当我们想打印控件内容时,如一个Grid中的内容,可以用WPF中PrintDialog类的PrintVisual()方法来实现
界面如下:
XAML代码如下
1 <Grid> 2 <Grid.ColumnDefinitions> 3 <ColumnDefinition/> 4 <ColumnDefinition Width="300"/> 5 </Grid.ColumnDefinitions> 6 <Grid Grid.Column="0"> 7 8 <Grid Width="800" Name="grid1"> 9 <TextBlock TextWrapping="Wrap" FontSize="15"> 10 Control authors who want to customize the arrange pass of layout processing should override this method. The implementation pattern should call M:System.Windows.UIElement.Arrange(System.Windows.Rect) on each visible child element, and pass the final desired size for each child element as the finalRect parameter. Parent elements should call M:System.Windows.UIElement.Arrange(System.Windows.Rect) on each child, otherwise the child elements will not be rendered. 11 Many derived classes offer implementations of this method. Prominent ones include: M:System.Windows.Window.ArrangeOverride(System.Windows.Size), M:System.Windows.Controls.Page.ArrangeOverride(System.Windows.Size) and M:System.Windows.Controls.Control.ArrangeOverride(System.Windows.Size). 12 </TextBlock> 13 </Grid> 14 </Grid> 15 16 <Grid Grid.Column="1"> 17 <Button Height="30" VerticalAlignment="Top" Click="Button_Click">打印</Button> 18 </Grid> 19 </Grid>
当我们点击按钮时,进行打印
按钮事件:
时间: 2024-10-03 22:37:58