WPF備忘錄六(資料繫結篇)
以下示例程式碼通過ListBox來繫結一個實體類的資料,如下:
1、新建一個類Student.cs檔案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Demo
{
public class Student : INotifyPropertyChanged
{
private string name;
public event PropertyChangedEventHandler PropertyChanged;
public Student(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set
{
this.name = value;
OnPropertyChanged("Name");
}
}
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
MainWindow.xaml
<Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:Demo"
Title="WPF資料繫結" Height="350" Width="525" WindowStyle="ToolWindow">
<Grid>
<Grid.Resources>
<src:Data x:Key="students"></src:Data>
<DataTemplate x:Key="stuDatas">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</Grid.Resources>
<ListBox ItemTemplate="{StaticResource stuDatas}" Height="23" HorizontalAlignment="Left" Margin="22,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="131">
<ListBox.ItemsSource>
<Binding Source="{StaticResource students }"></Binding>
</ListBox.ItemsSource>
</ListBox>
</Grid>
</Window>
MainWindow.xaml.cs檔案內容:
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;
using System.Windows.Media.Animation;
namespace Demo
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
相關文章
- WPF/C#:資料繫結到方法C#
- Java備忘錄《資料型別》Java資料型別
- Runtime備忘-資料結構資料結構
- 備忘錄六:Spring Boot + Swagger_UISpring BootSwaggerUI
- C#-WPF資料繫結基礎(一)C#
- 【翻譯】WPF中的資料繫結表示式
- wpf RelativeSource繫結
- WPF 資料繫結之ValidationRule資料校驗綜合Demo
- 備忘錄
- 【備忘錄】
- Eigen備忘錄
- Python 備忘錄Python
- RabbitMQ備忘錄MQ
- [WPF]資料繫結時為何會出現StringFormat失效ORM
- 在WPF中一種較好的繫結Enums資料方法
- C#WPF資料繫結模板化操作四步走C#
- (六)WPF資料驅動模式模式
- linux 備忘記錄Linux
- Java備忘錄《集合》Java
- 網站備忘錄網站
- 備忘錄模式(Memento)模式
- iOS混合開發庫(GICXMLLayout)六、資料繫結原理iOSXML
- 備忘錄:關於.net程式連線Oracle資料庫Oracle資料庫
- 資料繫結
- WPF 介面如何繫結Command
- WPF使用MVVM(二)-命令繫結MVVM
- Java備忘錄《“==” 和 “equals”》Java
- Dart 學習備忘錄Dart
- 19_備忘錄模式模式
- 開發 備忘錄 - 雜
- 將WPF內部繫結的控制元件和資料拉取出來控制元件
- WPF 基礎MvvM繫結的使用MVVM
- WPF使用MVVM(一)-屬性繫結MVVM
- 備忘錄:C#獲取微信小程式的雲資料庫中資料C#微信小程式資料庫
- Android備忘錄《幀動畫》Android動畫
- Android備忘錄《單例模式》Android單例模式
- Docker部署禪道備忘錄Docker
- 備忘錄八:Shiro核心類
- 第 22 章 備忘錄模式模式