日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF入门(一)——绑定Binding

發布時間:2024/9/21 asp.net 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF入门(一)——绑定Binding 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  實現WPF界面控件屬性與后臺數據屬性綁定。

  建立解決方案如下:

    

    

MainWindow添加

  一個ListView,顯示List,添加綁定語句:? ItemsSource="{Binding Test}“。

  兩個Button,增加List和清空List。

xaml代碼如下:

1 <Window x:Class="TESTBind.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid> 6 <ListView Height="100" HorizontalAlignment="Left" Margin="12,39,0,0" Name="listView1" VerticalAlignment="Top" Width="481" 7 ItemsSource="{Binding Test}"/> 8 <Button Content="List++" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 9 <Button Content="ClearList" Height="23" HorizontalAlignment="Left" Margin="124,12,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" /> 10 </Grid> 11 </Window> View Code

添加BindModel,定義需要綁定的變量

添加Microsoft.Practices.Prism.dll庫,引用using Microsoft.Practices.Prism.ViewModel;

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; using Microsoft.Practices.Prism.ViewModel;namespace TESTBind.Models {public class BindModel : NotificationObject{public BindModel(){test = new ObservableCollection<string>();}private ObservableCollection<string> test;public ObservableCollection<string> Test{get { return test; }set{test = value;this.RaisePropertyChanged("Test");}}} } View Code

為Test賦值,bindModel.Test = new ObservableCollection<string>(testList);

這里ObservableCollection提供數據更新,我之前用過List,但是只能更新一次,當綁定值第二次改變時,程序就會報錯。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using TESTBind.Models; 15 using System.Collections.ObjectModel; 16 17 namespace TESTBind 18 { 19 /// <summary> 20 /// MainWindow.xaml 的交互邏輯 21 /// </summary> 22 public partial class MainWindow : Window 23 { 24 public MainWindow() 25 { 26 InitializeComponent(); 27 Init(); 28 } 29 private void Init() 30 { 31 listNum = 1; 32 testList = new List<string>(); 33 bindModel = new BindModel(); 34 this.DataContext = bindModel; 35 } 36 private void button1_Click(object sender, RoutedEventArgs e) 37 { 38 testList.Add("WPF界面綁定" + listNum); 39 bindModel.Test = new ObservableCollection<string>(testList); 40 listNum++; 41 } 42 private int listNum; 43 private List<string> testList; 44 private BindModel bindModel; 45 46 private void button2_Click(object sender, RoutedEventArgs e) 47 { 48 testList.Clear(); 49 bindModel.Test = new ObservableCollection<string>(testList); 50 listNum = 1; 51 } 52 } 53 } View Code

?下載鏈接:http://pan.baidu.com/s/1cgcVKy

轉載于:https://www.cnblogs.com/jwxjjh/p/5700619.html

總結

以上是生活随笔為你收集整理的WPF入门(一)——绑定Binding的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。