如何使用绑定像代理? [英] How to use Binding like proxy?

查看:123
本文介绍了如何使用绑定像代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Setter Property="IsChecked">
    <Setter.Value>
        <MultiBinding>
            <!-- Get value for property -->
            <Binding Path="IsPressed" RelativeSource="{RelativeSource Self}" Mode="OneWay"/>
            <!-- Set value to ViewModel's property -->
            <Binding Path="Shift" Mode="OneWayToSource"/>
        </MultiBinding>
    </Setter.Value>
</Setter>

我需要使用2绑定属性:一个财产获得的价值和一个值设置为视图模型的财产。
我怎样才能实现这个场景?

I need to use 2 bindings for property: one to get value for property and one to set value to ViewModel's property. How I can realize this scenario?

推荐答案

您可以创建几个附加属性。一会是你结合靶,其次将包含您的代理绑定。例:然后在ProxySource的OnChange实现你会得到文本框为的UIElement,也可以从ProxySource读值,并将其写入ProxyTarget。
这并不是一个非常干净的形式给出的,但它应该工作。
如果你不能得到它的工作,以后我可以写一个完整的样本。
好吧,我实现的一切,这里有完整的源:

You can create a couple of attached properties. One will be the target of your binding, and second will contain binding for your proxy. Example: Then in ProxySource OnChange implementation you will get TextBox as UIElement, there you can read value from ProxySource and write it to ProxyTarget. This is not a very clean aproach, but it should work. If you can't get it working, I can write a complete sample later. Ok, I've implemented everything, here's complete source:

public class ViewModel : ViewModelBase
{
    string sourceText;
    public string SourceText
    {
        get { return sourceText; }
        set
        {
            if (sourceText == value) return;
            sourceText = value;
            System.Diagnostics.Debug.WriteLine("SourceText:" + value);
            RaisePropertyChanged("SourceText");
        }
    }

    string targetText;
    public string TargetText
    {
        get { return targetText; }
        set
        {
            if (targetText == value) return;
            targetText = value;
            System.Diagnostics.Debug.WriteLine("TargetText:" + value);
            RaisePropertyChanged("TargetText");
        }
    }
}

public static class AttachedPropertiesHost
{
    public static object GetProxySource(DependencyObject obj)
    {
        return obj.GetValue(ProxySourceProperty);
    }

    public static void SetProxySource(DependencyObject obj, object value)
    {
        obj.SetValue(ProxySourceProperty, value);
    }

    public static readonly DependencyProperty ProxySourceProperty =
            DependencyProperty.RegisterAttached(
                "ProxySource", typeof(object), typeof(AttachedPropertiesHost),
                new UIPropertyMetadata(null, ProxySourcePropertyPropertyChanged)
            );

    private static void ProxySourcePropertyPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        dependencyObject.Dispatcher.BeginInvoke(
                new { Dp = dependencyObject, NewValue = e.NewValue },
                args => SetProxyTarget(args.Dp, args.NewValue)
            );
    }

    public static object GetProxyTarget(DependencyObject obj)
    {
        return obj.GetValue(ProxyTargetProperty);
    }

    public static void SetProxyTarget(DependencyObject obj, object value)
    {
        obj.SetValue(ProxyTargetProperty, value);
    }

    public static readonly DependencyProperty ProxyTargetProperty =
            DependencyProperty.RegisterAttached("ProxyTarget", typeof(object), typeof(AttachedPropertiesHost));
    }

<TextBox Text="{Binding SourceText, UpdateSourceTrigger=PropertyChanged}" 
             WpfDataGridLayout:AttachedPropertiesHost.ProxySource="{Binding RelativeSource={RelativeSource Self}, Path=Text, UpdateSourceTrigger=PropertyChanged}"
             WpfDataGridLayout:AttachedPropertiesHost.ProxyTarget="{Binding TargetText, Mode=OneWayToSource}"
             />

和从控制台在编辑文本的输出:
SourceText:F
TargetText:F
SourceText:FH
TargetText:FH
SourceText:FHH
TargetText:FHH

And the output from console while editing textbox: SourceText:f TargetText:f SourceText:fh TargetText:fh SourceText:fhh TargetText:fhh

这篇关于如何使用绑定像代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆