以下是一些经典的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>
```
```csharp
using 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>
```
```csharp
using 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>
```
```csharp
using 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 编辑过