LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

经典的WPF入门实例

admin
2024年2月7日 23:15 本文热度 180

以下是一些经典的WPF入门实例,它们可以帮助你熟悉并理解WPF的基本概念和用法:

1. Hello, WPF!: 这个例子非常简单,它展示了一个包含一个按钮的窗口,并演示了如何处理按钮的点击事件。

```xaml<Window x:Class="MyWpfApp.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Hello, WPF!" Height="200" Width="300">    <Grid>        <Button Content="Click Me" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Center" />    </Grid></Window>```

```csharpusing System.Windows;

namespace MyWpfApp{    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }

       private void Button_Click(object sender, RoutedEventArgs e)        {            MessageBox.Show("Hello, WPF!");        }    }}```


2. 数据绑定: 这个例子展示了如何使用数据绑定来将数据与界面元素进行关联。它展示了一个简单的学生信息列表,并演示了如何通过数据绑定显示和编辑学生的姓名和年龄。

```xaml<Window x:Class="MyWpfApp.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Student List" Height="300" Width="400">    <StackPanel>        <ListBox x:Name="lstStudents" DisplayMemberPath="Name" SelectionChanged="lstStudents_SelectionChanged" />        <TextBox x:Name="txtName" Text="{Binding SelectedItem.Name, ElementName=lstStudents}" />        <TextBox x:Name="txtAge" Text="{Binding SelectedItem.Age, ElementName=lstStudents}" />    </StackPanel></Window>```

```csharpusing System.Collections.ObjectModel;using System.ComponentModel;using System.Windows;

namespace MyWpfApp{    public partial class MainWindow : Window    {        public ObservableCollection<Student> Students { get; set; }

       public MainWindow()        {            InitializeComponent();            DataContext = this;

           Students = new ObservableCollection<Student>            {                new Student { Name = "Alice", Age = 20 },                new Student { Name = "Bob", Age = 22 },                new Student { Name = "Charlie", Age = 19 }            };

           lstStudents.ItemsSource = Students;        }

       private void lstStudents_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)        {            CollectionViewSource.GetDefaultView(Students).Refresh();        }    }

   public class Student : INotifyPropertyChanged    {        private string name;        public string Name        {            get { return name; }            set            {                if (name != value)                {                    name = value;                    OnPropertyChanged(nameof(Name));                }            }        }

       private int age;        public int Age        {            get { return age; }            set            {                if (age != value)                {                    age = value;                    OnPropertyChanged(nameof(Age));                }            }        }

       public event PropertyChangedEventHandler PropertyChanged;

       protected virtual void OnPropertyChanged(string propertyName)        {            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));        }    }}```


3. 命令: 这个例子展示了如何使用命令来处理用户界面上的操作。它展示了一个包含两个按钮的窗口,点击按钮时分别触发自定义命令的执行。

```xaml<Window x:Class="MyWpfApp.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Command Example" Height="200" Width="300">    <StackPanel>        <Button Content="Say Hello" Command="{Binding SayHelloCommand}" />        <Button Content="Say Goodbye" Command="{Binding SayGoodbyeCommand}" />    </StackPanel></Window>```

```csharpusing System;using System.Windows;using System.Windows.Input;

namespace MyWpfApp{    public partial class MainWindow : Window    {        public ICommand SayHelloCommand { get; set; }        public ICommand SayGoodbyeCommand { get; set; }

       public MainWindow()        {            InitializeComponent();            DataContext = this;

           SayHelloCommand = new RelayCommand(SayHello);            SayGoodbyeCommand = new RelayCommand(SayGoodbye);        }

       private void SayHello(object parameter)        {            MessageBox.Show("Hello, WPF!");        }

       private void SayGoodbye(object parameter)        {            MessageBox.Show("Goodbye, WPF!");        }    }

   public class RelayCommand : ICommand    {        private Action<object> execute;        private Func<object, bool> canExecute;

       public event EventHandler CanExecuteChanged        {            add { CommandManager.RequerySuggested += value; }            remove { CommandManager.RequerySuggested -= value; }        }

       public RelayCommand(Action<object> execute)            : this(execute, null)        {        }

       public RelayCommand(Action<object> execute, Func<object, bool> canExecute)        {            this.execute = execute;            this.canExecute = canExecute;        }

       public bool CanExecute(object parameter)        {            return canExecute == null || canExecute(parameter);        }

       public void Execute(object parameter)        {            execute(parameter);        }    }}```

这些例子涵盖了WPF的基本概念和用法,其中包括界面组件的使用、数据绑定、命令等。通过学习和实践这些例子,你可以快速入门并开始开发自己的WPF应用程序。


该文章在 2024/2/7 23:15:35 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved