使用扩展方法打造一套简单的WPF/SL绑定机制

简介:

在昨天的博文《WPF/Silverlight的数据绑定设计的真糟糕》中,分析了三大应用场景下WPF/SL下绑定的缺陷。我的应用中有95%以上(甚至99%以上)的绑定都属于那三种应用场景,因此无法接受WPF/SL繁琐的绑定机制。骂不是目的,想办法解决问题是黑客精神的所在。知道缺陷之后,就可以来弥补。刚才花了两个小时时间用扩展方法写了一套新的WPF/SL绑定机制。

本文只是一份简单的草案和思路探索,可以证明了这种机制的可行性。最近时间紧张,暂时还无法给出一份完备的解决方案,有兴趣的朋友可以沿着本文的思路写一套更完善的扩展方法出来。

一、朴素的绑定需求

为什么要骂?因为有大量“朴素”的绑定需求,WPF/SL做的不好。比如下图:

界面

这个界面有四个编辑状态:None 代表不能编辑,DrawRegion 代表在图片上画框,DrawFrontPoint 代表在图片上画前景点,DrawBackgroundPoint 代表在图片上画背景点。用枚举 EditMode 来代表编辑状态,将 EditMode 存储在属性 EditModeValue 中:

public enum EditMode 

    None, 
    DrawRegion, 
    DrawFrontPoint, 
    DrawBackgroundPoint 
}

一个很简单的需求就是把 EditModeValue 和右边的几个按钮的状态关联起来,比如说,当 EditModeValue  为 DrawRegion 时,“绘制分割区域”按钮呈现按下状态(IsEnabled 为false),其它按钮类似。当按下某个按钮时,自动将 EditModeValue 改为某种状态,

数据绑定是解决这个问题的最佳方案。然而,WPF/SL下的数据绑定实在太不给力了,写这种数据绑定很繁琐,让人敬而远之。

二、新的方案

下面用反射+扩展方法来写一套更简洁的数据绑定方案。直接上代码,代码不足一百行,实现了绑定属性链的功能(没做完全的测试,可能有错误,谨慎使用):

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Reflection;

namespace Orc.Util 

    public static class BindingHelper 
    { 
        internal class PropertyChangedCallback 
        { 
            internal INotifyPropertyChanged Caller { get; set; } 
            internal String PropertyName { get; set; } 
            internal Action Callback { get; set; } 
            internal INotifyPropertyChanged Src { get; set; } 
            internal String Path { get; set; } 
            internal List<PropertyChangedCallback> CallbackList { get; set; } 
            internal void OnPropertyChanged(object sender, PropertyChangedEventArgs e) 
            { 
                if (e.PropertyName == PropertyName) 
                { 
                    // 移除事件,避免副作用 
                    if (CallbackList != null) 
                    { 
                        foreach (PropertyChangedCallback item in CallbackList) 
                        { 
                            if (item.Caller != null) 
                                item.Caller.PropertyChanged -= item.OnPropertyChanged; 
                        } 
                        CallbackList.Clear(); 
                    }

                    // 回调 
                    Callback();

                    // 添加事件 
                    AddCallback(Src, Callback, Path); 
                } 
            }

            internal static void AddCallback(INotifyPropertyChanged src, Action callback, String path) 
            { 
                if (src == null || callback == null || String.IsNullOrEmpty(path)) return;

                List<PropertyChangedCallback> list = new List<PropertyChangedCallback>(); 
                INotifyPropertyChanged current = src; 
                Type type = src.GetType(); 
                String[] pathes = path.Split('.'); 
                foreach (String item in pathes) 
                { 
                    if (current == null) return;

                    String name = item.Trim(); 
                    if (String.IsNullOrEmpty(name)) break;

                    PropertyChangedCallback c = new PropertyChangedCallback(); 
                    c.Callback = callback; 
                    c.Caller = current; 
                    c.Src = src; 
                    c.PropertyName = name; 
                    c.CallbackList = list; 
                    c.Path = path; 
                    current.PropertyChanged += c.OnPropertyChanged; 
                    list.Add(c);

                    PropertyInfo pi = type.GetProperty(name); 
                    if (pi == null) break;

                    Object p = pi.GetValue(current, null); 
                    INotifyPropertyChanged np = p as INotifyPropertyChanged; 
                    if (np == null) break;

                    current = np; 
                    type = np.GetType(); 
                } 
            } 
        }

        public static void SetBindingChain(this Object dst, INotifyPropertyChanged src, String path, Action callback) 
        { 
            if (src == null || callback == null || String.IsNullOrEmpty(path)) return; 
            PropertyChangedCallback.AddCallback(src, callback, path); 
        } 
    } 
}

SetBindingChain 就是用于绑定的扩展方法,src 是绑定源,path是绑定属性链,语法和属性调用语法类似(A.B.C.D),可以通过IDE提示写完后复制进去,callback是当属性发生变化后的回调事件,可以是lambda表达式。使用最好是在窗体Load时或某个事件时做个批量绑定。

也可以在这思路基础上写出多属性链绑定及多对象绑定扩展方法,本文就不写了,下面是原型:

        public static void SetBindingChain(this Object dst, INotifyPropertyChanged src, String[] pathes, Action callback) 
        { 
            throw new NotImplementedException(); 
        }

        public static void SetBindingChain(this Object dst, INotifyPropertyChanged src1, String path1, INotifyPropertyChanged src2, String path2, Action callback) 
        { 
            throw new NotImplementedException(); 
        }

        public static void SetBindingChain(this Object dst, INotifyPropertyChanged src1, String[] pathes1, INotifyPropertyChanged src2, String[] pathes2, Action callback) 
        { 
            throw new NotImplementedException(); 
        }

        public static void SetBindingChain(this Object dst, INotifyPropertyChanged src1, String path1, INotifyPropertyChanged src2, String path2, INotifyPropertyChanged src3, String path3, Action callback) 
        { 
            throw new NotImplementedException(); 
        }

        public static void SetBindingChain(this Object dst, INotifyPropertyChanged src1, String[] pathes1, INotifyPropertyChanged src2, String[] pathes2, INotifyPropertyChanged src3, String[] pathes3, Action callback) 
        { 
            throw new NotImplementedException(); 
        }

        public static void SetBindingChain(this Object dst, INotifyPropertyChanged src1, String path1, INotifyPropertyChanged src2, String path2, INotifyPropertyChanged src3, String path3, INotifyPropertyChanged src4, String path4, Action callback) 
        { 
            throw new NotImplementedException(); 
        }

        public static void SetBindingChain(this Object dst, INotifyPropertyChanged src1, String[] pathes1, INotifyPropertyChanged src2, String[] pathes2, INotifyPropertyChanged src3, String[] pathes3, INotifyPropertyChanged src4, String[] pathes4, Action callback) 
        { 
            throw new NotImplementedException(); 
        }

三、使用新方案进行数据绑定

EditModeValue属性:

private EditMode m_editModeValue = EditMode.None;

public EditMode EditModeValue 

    get { return m_editModeValue; } 
    set { 
        if (m_editModeValue == value) return; 
        m_editModeValue = value; 
        NotifyPropertyChanged("EditModeValue"); 
    } 
}

单向绑定:

this.SetBindingChain(this, "EditModeValue", 
   () => 
   { 
       btnDrawSegRegion.IsEnabled = EditModeValue != EditMode.DrawRegion; 
       btnDrawFPoints.IsEnabled = EditModeValue != EditMode.DrawFrontPoint; 
       btnDrawBPoints.IsEnabled = EditModeValue != EditMode.DrawBackgroundPoint; 
   });

然后是按下按钮时状态改变:

private void btnDrawSegRegion_Click(object sender, RoutedEventArgs e) 

    EditModeValue = EditMode.DrawRegion; 
}

private void btnDrawFPoints_Click(object sender, RoutedEventArgs e) 

    EditModeValue = EditMode.DrawFrontPoint; 
}

private void btnDrawBPoints_Click(object sender, RoutedEventArgs e) 

    EditModeValue = EditMode.DrawBackgroundPoint; 
}

搞定!

四、分析

使用扩展方法 SetBindingChain 可以简化 《WPF/Silverlight的数据绑定设计的真糟糕》 中所提到的前两种场景,使用方便了很多,且无学习成本,能够满足大部分的绑定需求。使用体验靠近了Flex的绑定方案,也有些区别:

(1)绑定写在后台,没有前台直观,同时,也不方便解耦;

(2)Flex是自动分析绑定链,这里还要手动输入绑定链,是一个比较大的遗憾;

尽管如此,相对于又难学又难用的WPF/SL源生绑定,要好太多了,简洁、明了、不用学、不用记、柔性、适合大部分应用场景。本文只是对属性链的绑定,如有其它需求,如对索引器的绑定等等还请自行扩展。

至于第三种场景,目前还没好的解决办法。

五、其它

喜欢简洁明了的解决方案,比如说上面界面中的打开图像功能,我的代码是:

private void btnOpen_Click(object sender, RoutedEventArgs e) 

    this.OpenImageFile((String path) => { 
        tbImgPath.Text = path; 
        EditModeValue = EditMode.None; 
    }); 
}

非常简单明了,不用拖个 OpenFileDialog 出来,也不用记忆 filter 的语法。当然,在背后有个扩展方法在默默的提供服务:

public static void OpenImageFile(this Window element, Action<String> callbackOnFilePath, String filter = "图像文件|*.bmp;*.jpg;*.gif;*.png") 

    String filePath; 
    OpenFileDialog dlg = new OpenFileDialog();

    dlg.Filter = filter; 
    dlg.FileOk += (object sender, CancelEventArgs e) => 
    { 
        filePath = dlg.FileName; 
        if(callbackOnFilePath != null) 
            callbackOnFilePath(filePath); 
    }; 
    dlg.ShowDialog(); 
}

本文转自xiaotie博客园博客,原文链接http://www.cnblogs.com/xiaotie/archive/2011/02/15/1955051.html如需转载请自行联系原作者


xiaotie 集异璧实验室(GEBLAB)

相关文章
WPF SL 获取RichTextBox 的内容(string)
WPF: private string GetText(RichTextBox richTextBox) {         TextRange textRange = new TextRange(richTextBox.
1333 0
|
前端开发
WPF&SL之Basic MVVM
转自http://www.cnblogs.com/itkm/archive/2012/03/30/2425732.html 初学MVVM的时候总觉得这玩意老神秘了。然后又莫名其妙的被网络上推荐了Prism框架,搞得乱七八糟,项目差点失去控制。
1031 0
|
C# 计算机视觉
让WPF和SL控件同时支持绑定和赋值
前台: 后台: using System;using System.Collections.Generic;using System.Linq;using System.
708 0
|
10天前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
|
4月前
|
C#
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
59 1
|
8月前
|
C# Windows
WPF技术之图形系列Polygon控件
WPF Polygon是Windows Presentation Foundation (WPF)框架中的一个标记元素,用于绘制多边形形状。它可以通过设置多个点的坐标来定义多边形的形状,可以绘制任意复杂度的多边形。
450 0
|
8月前
|
C# Windows
WPF技术之RichTextBox控件
WPF RichTextBox是Windows Presentation Foundation (WPF)中提供的一个强大的文本编辑控件,它可以显示富文本格式的文本,支持多种文本处理操作。
344 0
|
4月前
|
前端开发 C# 容器
浅谈WPF之控件拖拽与拖动
使用过office的visio软件画图的小伙伴都知道,画图软件分为两部分,左侧图形库,存放各种图标,右侧是一个画布,将左侧图形库的图标控件拖拽到右侧画布,就会生成一个新的控件,并且可以自由拖动。那如何在WPF程序中,实现类似的功能呢?今天就以一个简单的小例子,简述如何在WPF中实现控件的拖拽和拖动,仅供学习分享使用,如有不足之处,还请指正。
105 2