x:Type

来源:互联网 发布:人体工学椅有用吗 知乎 编辑:程序博客网 时间:2024/06/07 13:23
//MyButton.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;namespace WpfApplication1{    class MyButton : Button    {        public Type UserWindowType { get; set; }        protected override void OnClick()        {            base.OnClick();            Window win = Activator.CreateInstance(this.UserWindowType) as Window;            if (null != win)            {                win.ShowDialog();            }        }    }}
<!--Window1.xaml--><Window x:Class="WpfApplication1.Window1"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Window1" Height="300" Width="300">    <StackPanel Background="LightBlue">        <TextBox Margin="5"/>        <TextBox Margin="5"/>        <TextBox Margin="5"/>        <Button Content="OK" Margin="5"/>    </StackPanel></Window>
<!--MainWindow.xaml--><Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:local="clr-namespace:WpfApplication1"        Title="MainWindow" Height="350" Width="525">    <StackPanel>        <local:MyButton Content="Show" UserWindowType="{x:Type TypeName=local:Window1}" Margin="5"/>    </StackPanel></Window>



原创粉丝点击