WPF:使用VS2015 创建自定义项模板

vs自定义项模板,提供了可以让我们自定义任何xaml布局的窗口或者用户控件,就像使用WPF自带的窗口一样,方便重用。我所创建的项模板很简单,窗口继承自INotifyPropertyChanged接口,并实现方法,然后定义2个属性字段,下面开始步骤:

首先创建一个WPF项目:WpfApplication1,紧接着创建一个名为BaseWindow的Window。

<Window x:Class="WpfApplication1.BaseWindow"
        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:WpfApplication1"
        mc:Ignorable="d"  WindowStartupLocation="CenterScreen"
   SizeToContent="WidthAndHeight"
        Title="BaseWindow" Height="800" Width="800">
    <Window.Resources>
    </Window.Resources>
    <Grid>
        <StackPanel Margin="20">
        </StackPanel>
    </Grid>
</Window>

后台代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
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.Shapes;
using System.Windows.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// BaseWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BaseWindow : Window, INotifyPropertyChanged
    {
        public BaseWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #region 属性
        private Guid _id = Guid.NewGuid();
        public Guid Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                RaisedPropertyChanged("Id");
            }
        }
        private string _name = string.Empty;
        public string FirstName {
            get
            {
                return _name;
            }

            set
            {
                _name = value;
                RaisedPropertyChanged("FirstName");
            }
        }
        #endregion
        #region 当前线程调度器
        private static readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;
        #endregion

        #region 属性更改通知
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        protected void RaisedPropertyChanged(string property)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
        #endregion
    }
}
选择工具栏中的文件:


选择“导出模板”:

选择“项模板”:注意右侧画红线的部分说明。点击“下一步”:

选择创建的“BaseWindow":点击”下一步”:

因为我的是在wpf工程中使用,“选择项引用”这步什么都不选,如果选择了,会把引用一块打包到zip文件中。点击“下一步”:

输入“模板名称”,“模板说明”与“图标图像”可选,我这里使用了一个png的图像。 注意“输出位置”,这个位置就是到处的位置,点击“完成”按钮之后会生成到这个路径下,成功之后会自动打开该路径下的文件夹:

重新启动VS,打开原来的WpfApplication1项目,右键选中项目-添加-新建项:

列表中就会显示导出的项模板“DZPBaseWindow",就可以像普通的Window来使用它了。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中实现自定义模板的报表可以使用 FlowDocument 和 DocumentPaginator 类。FlowDocument 是一种可扩展标记语言(XAML)文档格式,用于呈现富文本内容,而 DocumentPaginator 类则可以将 FlowDocument 分页并打印成报表。 下面是一个基本的实现步骤: 1. 创建一个 WPF 应用程序,添加一个 FlowDocument 控件。 2. 设计并创建报表模板,包括表头、表格和页脚等内容,并使用 XAML 语言编写。 3. 在代码中加载报表模板,并将数据填充到其中。 4. 使用 DocumentPaginator 类将 FlowDocument 分页,并将其打印成报表。 以下是一些代码示例,演示如何使用 FlowDocument 和 DocumentPaginator 实现自定义模板的报表: ```csharp // 加载报表模板 var report = new FlowDocument(); var reader = new XamlReader(); report.Blocks.AddRange((IEnumerable<Block>)reader.Load(xamlString)); // 填充数据 var table = report.FindName("table") as Table; foreach (var row in data) { var tableRow = new TableRow(); tableRow.Cells.Add(new TableCell(new Paragraph(new Run(row.Name)))); tableRow.Cells.Add(new TableCell(new Paragraph(new Run(row.Value)))); table.Rows.Add(tableRow); } // 分页并打印成报表 var paginator = ((IDocumentPaginatorSource)report).DocumentPaginator; var dlg = new PrintDialog(); if (dlg.ShowDialog() == true) { dlg.PrintDocument(paginator, "Report"); } ``` 需要注意的是,在实际应用中,可能需要将数据绑定到模板中的控件,或者使用其他类似的技术实现更复杂的报表功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值