<Window x:Class="WpfApp2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp2" mc:Ignorable="d" Name="thisWin" Title="MainWindow" > <Grid Background="AliceBlue"> <ScrollViewer Name="myscrollvier" CanContentScroll="True" VerticalScrollBarVisibility="Auto" > <StackPanel Name="PrintContainer" > <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> <TextBox></TextBox> </StackPanel> </ScrollViewer> <Button Content="Print" Width="50" Height="20" Click="btnPrint" /> </Grid> </Window>
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Xml.Serialization; namespace WpfApp2 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); var t1 = new List<object>() { new { Key = "肯肯肯" }, new { Key = "吃嘱吃" } }; Listbox_ItemsSource = t1; } public event PropertyChangedEventHandler PropertyChanged; private void RisePropertyChanged(string varName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(varName)); } private List<object> _Listbox_ItemsSource; /// <summary> /// listbox itemsSource /// </summary> public List<object> Listbox_ItemsSource { get { return _Listbox_ItemsSource; } set { _Listbox_ItemsSource = value; RisePropertyChanged("Listbox_ItemsSource"); } } private void btnPrint(object sender, RoutedEventArgs e) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { var control = CloneControl(myscrollvier); System.Printing.PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket); double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / myscrollvier.ActualWidth, capabilities.PageImageableArea.ExtentHeight / myscrollvier.ActualHeight); control.LayoutTransform = new ScaleTransform(scale, scale); Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); //update the layout of the visual to the printer page size. control.Measure(sz); control.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); printDialog.PrintVisual(control, "123"); } } public T CloneControl<T>(T control) where T : Control, new() { string xaml = System.Windows.Markup.XamlWriter.Save(control); T rtb2 = System.Windows.Markup.XamlReader.Parse(xaml) as T; return rtb2; } } }