07
2014
01

WPF入门练习01-bypkm

说到wpf,我之前以为它会很难,不过学了一点知识之后,我觉得和winform差不多,最大的差别就是wpf前台布局是使用相当于XML的语言来编写的,不过学了一下也就懂了。

下面就是练习的两个简单的例子:

例子1,生成10行10列,随机放图片:

前台布局:

<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" Icon="/WpfApplication1;component/Images/logo.ico" Loaded="Window_Loaded">
    <Grid>
        <!--<StackPanel>
            <Button Width="70px"  Content="btn1" Name="btn1"></Button>
            <ProgressBar Margin="10px " Height="13" Minimum="0" Maximum="100" Value="70" Name="progressBar1" Width="284" />
        </StackPanel>-->
        <Grid Name="grid1">
            <!--行和列定义-->
            <!--<Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Content="1行1列" Grid.Row="1" Grid.Column="1" Margin="0,0,44,68"></Button>-->
        </Grid>
    </Grid>
</Window>


后台代码:

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button)
            {
                Button bt = sender as Button;
                MessageBox.Show(bt.Name);
            }
            else
            {
                MessageBox.Show("其他");
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                ColumnDefinition colum=new ColumnDefinition();
                grid1.ColumnDefinitions.Add(colum);
                RowDefinition row = new RowDefinition();
                grid1.RowDefinitions.Add(row);
            }
            Random rd = new Random();
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    //Button btn = new Button();
                    //btn.Content = i+","+j;
                    //Grid.SetRow(btn, i);
                    //Grid.SetColumn(btn, j);
                    //grid1.Children.Add(btn);
                    Image img = new Image();
                    img.Source = new BitmapImage(new Uri(string.Format("Images/{0}.gif",rd.Next(1,11)),UriKind.Relative));
                    Grid.SetRow(img, i);
                    Grid.SetColumn(img, j);
                    grid1.Children.Add(img);
                }
            }
        }
    }

效果:



例子2 菜单和工具栏控件的使用:

前台布局:

<Window x:Class="WpfControl1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="主窗口" Height="350" Width="525" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Icon="/WpfControl1;component/Images/logo.ico">
    <Grid>
        <DockPanel>
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="文件">
                    <MenuItem Header="打开" Name="MI_Open" Click="MI_Open_Click">
                        <MenuItem Header="1.txt" Click="MI_Open_Click"></MenuItem>
                        <MenuItem Header="2.txt" Click="MI_Open_Click"></MenuItem>
                        <MenuItem Header="3.txt" Click="MI_Open_Click"></MenuItem>
                        <MenuItem Header="选择文件…" Name="MI_OpenFile" Click="MI_Open_Click"></MenuItem>
                    </MenuItem>
                    <MenuItem Header="保存" Name="MI_SaveFile" Click="MI_Open_Click"></MenuItem>
                </MenuItem>
                <MenuItem Header="编辑"></MenuItem>
                
            </Menu>
            <ToolBar DockPanel.Dock="Top">
                <Button Content="新建"></Button>
                <Button Content="打开" Name="btnOpenWin" Click="btnOpenWin_Click"></Button>
                <CheckBox Content="是否选择"></CheckBox>
            </ToolBar>
            <TextBox DockPanel.Dock="Bottom" TextWrapping="Wrap" Name="txtContent"></TextBox>
        </DockPanel>
    </Grid>
</Window>


后台代码:

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private string  FilePath="";
        private void MI_Open_Click(object sender, RoutedEventArgs e)
        {
            if (sender is MenuItem)
            {
                MenuItem mi = sender as MenuItem;
                //MessageBox.Show(mi.Name);
                switch (mi.Name)
                {
                    case "MI_OpenFile":
                        {
                            OpenFileDialog ofd = new OpenFileDialog();
                            ofd.Filter = "文本文件(aspx,html,txt)|*.txt;*.html;*.aspx|所有文件|*.*";
                            ofd.FilterIndex = 0;
                            ofd.Multiselect = true;
                            ofd.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
                            if(ofd.ShowDialog()==true)
                            {
                               // MessageBox.Show(ofd.FileName);
                                FilePath = ofd.FileName;
                                txtContent.Text = System.IO.File.ReadAllText(FilePath,Encoding.Default);
                            }
                        }
                        break;
                    case "MI_SaveFile":
                        {
                            
                            SaveFileDialog sfd = new SaveFileDialog();
                            sfd.Filter = "文本文件(aspx,html,txt)|*.txt;*.html;*.aspx|所有文件|*.*";
                            sfd.FilterIndex = 0;
                            if (FilePath != "")
                            {
                                sfd.InitialDirectory = System.IO.Path.GetFullPath(FilePath);
                            }
                            else
                            {
                                sfd.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
                            }
                            if (sfd.ShowDialog() == true)
                            {
                                System.IO.File.WriteAllText(sfd.FileName, txtContent.Text,Encoding.Default);
                            }
                        }
                        break;
                    default: 
                        break;
                }
            }
            else
            {
                MessageBox.Show("不是菜单项。");
            }
        }
        //打开一个新窗口,与winform一样
        private void btnOpenWin_Click(object sender, RoutedEventArgs e)
        {
            //SecondWin win2 = new SecondWin("第二窗口");
            //win2.ShowActivated = true;
            //win2.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //bool? result=win2.ShowDialog();
            //if (result != null)
            //{
            //    MessageBox.Show(result.ToString());
            //}
        }
    }


效果:



版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

相关文章:

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。