博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义绑定2
阅读量:5914 次
发布时间:2019-06-19

本文共 4369 字,大约阅读时间需要 14 分钟。

using System; using System.ComponentModel; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Windows; using System.Windows.Data; namespace BindingSample {
class Program {
static void Main(string[] args) {
//1. Test Binding View view1 = new View(); View view2 = new View(); ViewModel model = new ViewModel(); ViewModel2 model2 = new ViewModel2(); TextConverter converter = new TextConverter(); BindingEngine.SetPropertyBinding(view1, model, "Text", "Value", converter, null); BindingEngine.SetPropertyBinding(view1, model, "Value", "Value"); BindingEngine.SetPropertyBinding(view2, model, "Text", "Value", converter, 2); BindingEngine.SetPropertyBinding(view2, model, "Value", "Value"); BindingEngine.SetPropertyBinding(view2, model2, "Value2", "Value"); Console.WriteLine("Set Binding to view1 and view2"); Console.WriteLine("view1 Text = {0} \t Value = {1} \nview2 Text = {2} \t Value = {3} \t Value2 = {4}", view1.Text, view1.Value, view2.Text, view2.Value, view2.Value2); model.Value = 1; model2.Value = 4; Console.WriteLine("\nSet model.Value = 1, model2 = 4"); Console.WriteLine("view1 Text = {0} \t Value = {1} \nview2 Text = {2} \t Value = {3} \t Value2 = {4}", view1.Text, view1.Value, view2.Text, view2.Value, view2.Value2); Console.WriteLine("\nClear binding of view1"); BindingEngine.ClearPropertyBinding(view1, model, "Text", "Value"); BindingEngine.ClearPropertyBinding(view1, model, "Value", "Value"); model.Value = 3; Console.WriteLine("\nSet model.Value = 3"); Console.WriteLine("view1 Text = {0} \t Value = {1} \nview2 Text = {2} \t Value = {3}", view1.Text, view1.Value, view2.Text, view2.Value); //2. Test memory leak View view3 = new View(); ViewModel model3 = new ViewModel(); WeakReference wr = new WeakReference(view3); BindingEngine.SetPropertyBinding(view3, model3, "Text", "Value", converter, null); BindingEngine.SetPropertyBinding(view3, model3, "Value", "Value"); Console.WriteLine("\nSet Binding to view3"); Console.WriteLine("The view3 is alive : {0}", wr.IsAlive); view3 = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Console.WriteLine("Set view3 = null , after GC Collect"); Console.WriteLine("The view3 is alive : {0}", wr.IsAlive); Console.Read(); } } public class View {
public string Text { get; set; } public int Value { get; set; } public int Value2 { get; set; } } public class ViewModel : INotifyPropertyChanged {
private int _Value = 0; public int Value {
get {
return _Value; } set {
_Value = value; NotifyPropertyChanged("Value"); } } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string prop) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } } public class ViewModel2 : INotifyPropertyChanged {
private int _Value = 0; public int Value {
get {
return _Value; } set {
_Value = value; NotifyPropertyChanged("Value"); } } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string prop) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } } public class TextConverter : IDataConverter {
public object Convert(object value, object parameter) {
int v = System.Convert.ToInt32(value); if (parameter != null) {
v = System.Convert.ToInt32(parameter) + v; } return string.Format("\"{0}\"", v); } } }

 

转载于:https://www.cnblogs.com/shenfengok/archive/2012/01/12/2320325.html

你可能感兴趣的文章
性能测试分享:性能测试工具开发的案例分享(下)
查看>>
linux sar命令详解
查看>>
通过Gearman实现MySQL到Redis的数据复制
查看>>
eclipse 自动为getter和setter添加注释
查看>>
我的友情链接
查看>>
DataSet
查看>>
Quartz.NET 前一次任务未执行完成时不触发下次的解决方法
查看>>
python unittest之断言及示例
查看>>
online_judge_1106
查看>>
JAVA_内部类
查看>>
jxl 导入excel
查看>>
虚拟机linux上网问题
查看>>
XMLHttpRequest - 原始AJAX初步
查看>>
laravel/lumen 单元测试
查看>>
csu2161: 漫漫上学路(Hash+最短路)
查看>>
重复引用错误:duplicate symbols for architecture x86_64
查看>>
ucenter1.5通讯过程分析(转载)
查看>>
浏览器中可以访问,但是git命令、go get命令使用时却无法连接
查看>>
Apache Spark源码走读之7 -- Standalone部署方式分析
查看>>
如何避免重构带来的危险
查看>>