我正在寻找一种方法来查找Window类型的所有控件,
例如:查找全部TextBoxes,找到实现特定接口的所有控件等.
我正在制作一个用于打印标签的 WPF 应用程序。我想设计一个标签模板作为 WPF 窗口。在另一个类中,我将实例化这个“窗口模板”,在运行时填充属性并打印标签。我无法在打印前在屏幕上显示标签,因此无法在此窗口实例上调用 .ShowDialog()。这在后面发挥作用。
过去一周我一直在做这方面的研究,我发现了两种几乎可以分别做我想做的事情的方法,如果我可以将它们结合起来就可以了,但是我遗漏了一部分。
我可以让它工作,但按照这里的步骤 在 WPF 第 2 部分中打印
这样就完成了打印文档的基本操作。但是,我将无法使用我的模板,我必须将所有内容放在后面的代码中。这是一个可行的选择,但我想更多地探索模板的想法。
PrintDialog pd = new PrintDialog();
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new System.Windows.Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
FixedPage page1 = new FixedPage();
page1.Width = document.DocumentPaginator.PageSize.Width;
page1.Height = document.DocumentPaginator.PageSize.Height;
// add some text to the page
Label _lblBarcode = new Label();
_lblBarcode.Content = BarcodeConverter128.StringToBarcode(palletID);
_lblBarcode.FontFamily = new System.Windows.Media.FontFamily("Code 128");
_lblBarcode.FontSize = 40;
_lblBarcode.Margin = new Thickness(96);
page1.Children.Add(_lblBarcode);
// add the page to the document
PageContent page1Content = …Run Code Online (Sandbox Code Playgroud) 以最简单的形式...
我想创建尽可能多的 StackPanel,然后在其中添加矩形。然后,例如,当我单击“开始”按钮时,能够更改任何一个矩形的填充颜色。一切都在代码隐藏中。
任何帮助,将不胜感激。
例如,如果我们最喜欢的啤酒编写了框架,我可以这样做:
XAML:
<Page
x:Class="Test2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Name="StartButton" Content="Start" Click="StartButton_Click" Height="30" Width="200" Margin="5"/>
</StackPanel>
<StackPanel Grid.Row="1" Name="myStackPanel" VerticalAlignment="Top"/>
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)
背后代码:
namespace Test2
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
for (var i = 0; i < 5; i++) // The 5 here could be any number
{
myStackPanel.Children.Add(new StackPanel
{
Name = "myPanel" …Run Code Online (Sandbox Code Playgroud)