高级控件:菜单、工具栏多窗口控制

来源:互联网 发布:tgp腾讯游戏客户端mac 编辑:程序博客网 时间:2024/06/06 00:13



<Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525">    <Grid>        <DockPanel>            <Menu DockPanel.Dock="Top">                <MenuItem Header="文件">                    <MenuItem Header="打开" Name="miOpen" Click="miOpen_Click"></MenuItem>                </MenuItem>                <MenuItem Header="编辑">                    <MenuItem Header="复制"></MenuItem>                </MenuItem>            </Menu>            <TextBox DockPanel.Dock="Bottom"></TextBox>        </DockPanel>    </Grid></Window>



<Window x:Class="WpfApplication2.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        WindowState="Maximized"        Title="主程序" Height="350" Width="525">    <Grid>        <DockPanel>                        <Menu DockPanel.Dock="Top">                <MenuItem Header="打开"></MenuItem>                <MenuItem Header="编辑"></MenuItem>            </Menu>            <ToolBar DockPanel.Dock="Top" Height="30">                <Button Name="btnDelete" Click="btnDelete_Click">                    <Image Source="Images/delete.ico"></Image>                </Button>                <Button>                    <Button.Content>                        <Image Source="Images/clipboard.ico"></Image>                    </Button.Content>                </Button>                <CheckBox Content="自动保存"></CheckBox>                <TextBox></TextBox>            </ToolBar>            <TextBox Name="txtName" DockPanel.Dock="Bottom"></TextBox>        </DockPanel>            </Grid></Window>

<Application x:Class="WpfApplication2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

在APP.xaml里StartupUri="MainWindow.xaml"是指定主窗口的属性


<Window x:Class="WpfApplication2.AboutWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="关于" Height="500" Width="800" ResizeMode="NoResize"        WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closed="Window_Closed">    <Grid>        <TextBox Name="txtTest" Height="23" HorizontalAlignment="Left" Margin="58,91,0,0" VerticalAlignment="Top" Width="120" Text="我的对话框" />        <PasswordBox Height="23" HorizontalAlignment="Left" Margin="69,126,0,0" Name="pwdBox1" VerticalAlignment="Top" Width="120" />    </Grid></Window>
ResizeMode="NoResize"<span style="white-space:pre"></span>使其无法修改大小
WindowStartupLocation="CenterScreen" <span style="white-space:pre"></span>窗口打开的位置在正中央
WindowState="maxsize" <span style="white-space:pre"></span>让窗口打开时最大化




相互传值:

using System;using System.Collections.Generic;using System.Linq;using System.Text;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;namespace WpfApplication2{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void btnDelete_Click(object sender, RoutedEventArgs e)        {            string name = txtName.Text;            AboutWindow aboutWin = new AboutWindow();            aboutWin.UserName = name;//无论是子窗口向主窗口传值            //还是子窗口给主窗口传值,都是通过属性            aboutWin.ShowDialog();            MessageBox.Show(aboutWin.Password);        }    }}


using System;using System.Collections.Generic;using System.Linq;using System.Text;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;namespace WpfApplication2{    /// <summary>    /// AboutWindow.xaml 的交互逻辑    /// </summary>    public partial class AboutWindow : Window    {        public string UserName { get; set; }        public string Password { get; set; }        public AboutWindow()        {            InitializeComponent();        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            txtTest.Text = UserName;        }        private void Window_Closed(object sender, EventArgs e)        {            Password = pwdBox1.Password;        }//传递数值    }}


     private void button3_Click(object sender, RoutedEventArgs e)        {            //openfiledialog ofd = new openfiledialog();            //ofd.filter = "文本文件|*.txt|png图片|*.png|视频|*.avi|所有文件|*.*";            //if (ofd.showdialog() == true)            //{            //    string file = ofd.filename;//打开的文件名            //    messagebox.show("打开文件" + file);            //}            //else            //{            //    messagebox.show("取消了");            //}            SaveFileDialog sfd = new SaveFileDialog();            sfd.Filter = "文本文件|*.txt|word文档|*.doc";            if (sfd.ShowDialog() == true)            {                //会自动加后缀扩展名                MessageBox.Show(sfd.FileName);            }        }
<span style="white-space:pre">        private void btnChoosePic_Click(object sender, RoutedEventArgs e)        {            OpenFileDialog ofd = new OpenFileDialog();            ofd.Filter = "JPEG图片|*.jpg|PNG图片|*.png|BMP图片|*.bmp";            if (ofd.ShowDialog() == true)            {                string picFileName = ofd.FileName;                image1.Source = new BitmapImage(new Uri(picFileName));            }        }</span>


0 0
原创粉丝点击