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();
}
}
}
相關文章
- Flex 資料繫結備忘Flex
- WPF備忘錄四(事件動畫)事件動畫
- WPF多表關聯資料繫結
- Java備忘錄《資料型別》Java資料型別
- C#-WPF資料繫結基礎(一)C#
- WPF入門教程系列十八——WPF中的資料繫結(四)
- 【翻譯】WPF中的資料繫結表示式
- Runtime備忘-資料結構資料結構
- WPF 繫結基礎
- 備忘錄
- 菜鳥初學Java的備忘錄(六) (轉)Java
- WPF快速指導4:資料繫結之繫結方向與自定義轉換器
- MVVM模式和在WPF中的實現(二)資料繫結MVVM模式
- 備忘錄--最近一些總結
- 備忘錄六:Spring Boot + Swagger_UISpring BootSwaggerUI
- 備忘錄模式模式
- iOS備忘錄iOS
- JUnit備忘錄
- CSS備忘錄CSS
- C#WPF資料繫結模板化操作四步走C#
- 在WPF中一種較好的繫結Enums資料方法
- WPF使用MVVM(二)-命令繫結MVVM
- WPF技巧(1)非同步繫結非同步
- Python 備忘錄Python
- Java備忘錄《集合》Java
- 網站備忘錄網站
- rman命令備忘錄
- iOS混合開發庫(GICXMLLayout)六、資料繫結原理iOSXML
- 備忘錄:關於.net程式連線Oracle資料庫Oracle資料庫
- 資料繫結
- WPF使用MVVM(一)-屬性繫結MVVM
- WPF 介面如何繫結Command
- WPF 基礎MvvM繫結的使用MVVM
- 簡單資料繫結和複雜資料繫結
- 備忘錄模式(Memento)模式
- Java備忘錄《“==” 和 “equals”》Java
- Dart 學習備忘錄Dart
- linux 備忘記錄Linux