原文 http://www.11011.net/wpf-binding-properties

Ever wanted to write the following?

<RichTextBoxDocument="{Binding}" />

I noticed at least one user on the MSDN Forums who did. The general answer is that it's not possible - because Document isn't a DependencyProperty. Sometime last year I wrote a utility that I hoped would solve this very problem. Back then, it didn't work properly, but with increased WPF experience I gave it another shot. This time it seems to pass the basic tests I throw at it.

The concept is a "Proxy" FrameworkElement with two DependencyProperties, one Input and one Output. The Input property is tied to the Output such that when Input changes it is applied to Output.

<utils:ProxyIn="{Binding}"Out="{Binding ElementName=richTextBox, Path=Document}" />

This effectively binds the Document property of a RichTextBox. If the above doesn't make sense it's probably due to the default settings on the Out property. Namely BindsTwoWayByDefault and UpateSourceTrigger.PropertyChanged.

I'll post the entire Proxy source at the end of this entry, but for now let's step through some of the more interesting details.

FrameworkPropertyMetadata inMetadata = new FrameworkPropertyMetadata(
delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
{
(p as Proxy).Out = args.NewValue;
});

The PropertyChangedCallback for the In property does as you probably expected, it just sets the new value on the Out property.

But we also need a PropertyChangedCallback for the Out property. I wanted the Proxy to bind two-way by default so that in the event the source (the non-DependencyProperty) changed, the proxy would overwrite the change with the In value. In some cases it is also necessary to overwrite the initial value. If the In property changes or is bound before Out is bound the In value is not always propagated. Fortunately when Out is bound it calls its own PropertyChangedCallback allowing us to propagate the initial value.

FrameworkPropertyMetadata outMetadata = new FrameworkPropertyMetadata(
delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
{
Proxy proxy = p as Proxy;
object expected = proxy.In;
if (!object.ReferenceEquals(args.NewValue, expected))
{
Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Background,
new Operation(delegate
{
proxy.Out = proxy.In;
}));
}
});

The PropertyChangedCallback for Out does just that. It checks if Out is the same as In and if not asynchronously (so as not to confuse the binding engine) overwrites Out with In.

As promised, here is the complete source code.

public class Proxy : FrameworkElement
{
public static readonly DependencyProperty InProperty;
public static readonly DependencyProperty OutProperty; public Proxy()
{
Visibility = Visibility.Collapsed;
} static Proxy()
{
FrameworkPropertyMetadata inMetadata = new FrameworkPropertyMetadata(
delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
{
if (null != BindingOperations.GetBinding(p, OutProperty))
(p as Proxy).Out = args.NewValue;
}); inMetadata.BindsTwoWayByDefault = false;
inMetadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; InProperty = DependencyProperty.Register("In",
typeof(object),
typeof(Proxy),
inMetadata); FrameworkPropertyMetadata outMetadata = new FrameworkPropertyMetadata(
delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
{
ValueSource source = DependencyPropertyHelper.GetValueSource(p, args.Property); if (source.BaseValueSource != BaseValueSource.Local)
{
Proxy proxy = p as Proxy;
object expected = proxy.In;
if (!object.ReferenceEquals(args.NewValue, expected))
{
Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.DataBind, new Operation(delegate
{
proxy.Out = proxy.In;
}));
}
}
}); outMetadata.BindsTwoWayByDefault = true;
outMetadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; OutProperty = DependencyProperty.Register("Out",
typeof(object),
typeof(Proxy),
outMetadata);
} public object In
{
get { return this.GetValue(InProperty); }
set { this.SetValue(InProperty, value); }
} public object Out
{
get { return this.GetValue(OutProperty); }
set { this.SetValue(OutProperty, value); }
}
}

And finally, a complete example.

<Windowx:Class="PropertyBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:utils="clr-namespace:"
>
<Grid>
<Grid.DataContext>
<FlowDocument>
<Paragraph>Bind <Bold>This!</Bold></Paragraph>
</FlowDocument>
</Grid.DataContext>
<RichTextBoxHeight="200"Name="rtb" />
<utils:ProxyIn="{Binding}"Out="{Binding ElementName=rtb, Path=Document}" />
</Grid>
</Window>

Impossible WPF Part 1: Binding Properties的更多相关文章

  1. Impossible WPF Part 2: Binding Expressions

    原文 http://www.11011.net/wpf-binding-expressions Back in April I posted an idea for building an expre ...

  2. 【转】WPF中的Binding技巧(二)

    WPF中的Binding技巧(二)     接上篇, 我们来看一看Elementname,Source,RelativeSource 三种绑定的方式 1.ElementName顾名思义就是根据Ui元素 ...

  3. [XAML]类似WPF绑定的Binding的读取方法

    在WPF的XAML里,依赖属性可以使用基于BindingBase之类的MarkupExtensin 读取XAML时,会自动的把该BindingBase转换为BindingExpressionBase ...

  4. [WPF系列]-使用Binding来同步不同控件的Dependency property

    简介 项目中经常会用到,同步两个控件的值,本文就简单列举两种方式来同步不同控件的两个Dependency Property. 示例 效果图: 只使用C#代码: //获取slider1的ValueDep ...

  5. (WPF) 再议binding:点击User Control时,User Control变换颜色或做其他的处理。

    Binding 是前台UI(显示层)和后台代码(数据层)的桥梁.理论上当后台的数据变动时,显示的数据或样式应该随之而变.这些是动态的. 对于Binding的设置可以在前台Xaml,也可以在后台Code ...

  6. 解读WPF中的Binding

    1.Overview 基于MVVM实现一段绑定大伙都不陌生,Binding是wpf整个体系中最核心的对象之一这里就来解读一下我花了纯两周时间有哪些秘密.这里我先提出几个问题应该是大家感兴趣的,如下: ...

  7. (WPF, MVVM) Textbox Binding

    参考:http://msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger(v=vs.110).aspx Te ...

  8. (WPF, MVVM) Slider Binding.

    对于Button的Command的绑定可以通过实现ICommand接口来进行,但是Slider并没有Command属性. 另外如果要实现MVVM模式的话,需要将一些Method和Slider的Even ...

  9. (WPF) MVVM: DataGrid Binding

    Binding到DataGrid的时候,需要用到ObservableCollection. public ObservableCollection<Customer> Customers ...

随机推荐

  1. Linux的默认编码可以通过export LC_ALL=zh_CN.GBK来修改

    http://www.cnblogs.com/malecrab/p/5300486.html

  2. 手工部署Sqlserver CLR程序集

    原文 手工部署Sqlserver CLR程序集 以前一直用VS部署Sqlserver CLR程序集简单省事,现在服务器部署在内网了,必须手动更新部署Sqlserver CLR程序集.    开始以为A ...

  3. live555学习经验链接二

    live555学习经验链接二:http://blog.csdn.net/nkmnkm/article/category/1066093/2

  4. dojo 学习笔记

    1  因为Dijit包括了一系列的UI组件,他绑定了4个支持的主题:nihilo, soria, tundra 和claro.每个主题包括了一系列的图片和CSS文件来控制组件的外观.CSS文件必须显示 ...

  5. 九款让WordPress成为赚钱利器的广告插件

    Blog有了很不错的流量后,看到别人博客挂的广告挣$,是否也有挂广告的冲动,但是,修改wordpress模版去让人不厌其烦,布局.样式都的重新修改一下,为了不那么麻烦,笔者整理的几款wordpress ...

  6. Cocos2d-x C++调用Android弹出提示框

    转载请注明地址,谢谢.. Cocos2d-x中提供了一个JniHelper类来让我们对Jni进行操作. (PS:弄了一天想自己写代码操作Jni的,但是总是出错,技术差不得不使用Cocos2d-x现成的 ...

  7. Dropping tests(01分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8176   Accepted: 2862 De ...

  8. Java发送HTTP POST请求(内容为xml格式)

    今天在给平台用户提供http简单接口的时候,顺便写了个调用的Java类供他参考.      服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的h ...

  9. SharePoint 2010 BCS - 简单实例(一)数据源加入

    博客地址 http://blog.csdn.net/foxdave 本篇基于SharePoint 2010 Foundation. 我的数据库中有一个病人信息表Patient,如今我就想把这个表中的数 ...

  10. ps快速删除圆角图片旁白的白色区域方法

    简单实用5招的ps快速删除圆角图片旁白的白色区域方法 1.图像-模式-rgb颜色 2.双击背景取消图层锁定 3.用魔棒工具点击要删除的区域 4.delete删除 5.另存为png图片