以下是一些經(jīng)典的WPF入門(mén)實(shí)例,它們可以幫助你熟悉并理解WPF的基本概念和用法:
1. Hello, WPF!: 這個(gè)例子非常簡(jiǎn)單,它展示了一個(gè)包含一個(gè)按鈕的窗口,并演示了如何處理按鈕的點(diǎn)擊事件。
```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. 數(shù)據(jù)綁定: 這個(gè)例子展示了如何使用數(shù)據(jù)綁定來(lái)將數(shù)據(jù)與界面元素進(jìn)行關(guān)聯(lián)。它展示了一個(gè)簡(jiǎn)單的學(xué)生信息列表,并演示了如何通過(guò)數(shù)據(jù)綁定顯示和編輯學(xué)生的姓名和年齡。
```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. 命令: 這個(gè)例子展示了如何使用命令來(lái)處理用戶(hù)界面上的操作。它展示了一個(gè)包含兩個(gè)按鈕的窗口,點(diǎn)擊按鈕時(shí)分別觸發(fā)自定義命令的執(zhí)行。
```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的基本概念和用法,其中包括界面組件的使用、數(shù)據(jù)綁定、命令等。通過(guò)學(xué)習(xí)和實(shí)踐這些例子,你可以快速入門(mén)并開(kāi)始開(kāi)發(fā)自己的WPF應(yīng)用程序。
該文章在 2024/2/7 23:15:35 編輯過(guò)