查看物业 - 两次绑定 [英] View property - bind twice

查看:112
本文介绍了查看物业 - 两次绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能控制的相同属性绑定不止一次?

要例如:

 <弹出ISOPEN ={绑定路径=(本地:ListViewBehavior.IsColumnHeaderClicked)
    的RelativeSource = {的RelativeSource FindAncestor,AncestorType = GridViewColumnHeader}}...

正如你所看到 Popup.IsOpen 势必附加属性。我想将其绑定到视图模型 IsPopupOpened ,但不知道怎么样。


试图@Arhiman答案没有成功:

 < Popup.IsOpen>
    < MultiBinding转换器={本地:MultiBindingConverter}>
        <绑定路径=(本地:ListViewBehavior.IsColumnHeaderClicked)
                 的RelativeSource ={的RelativeSource FindAncestor,AncestorType = GridViewColumnHeader}/>
        <绑定路径=DataContext.IsPopupId
                 的RelativeSource ={的RelativeSource FindAncestor,AncestorType =用户控件}/>
    < / MultiBinding>
< /Popup.IsOpen>

天真的转换器逻辑:

 公共类MultiBindingConverter:的MarkupExtension,IMultiValueConverter
{
    公共MultiBindingConverter(){}    公众覆盖对象ProvideValue(的IServiceProvider的ServiceProvider)=>这个;    [对象] _old;    公共对象转换(对象[]值类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
        如果(_old == NULL)
            _old = values​​.ToArray();
        //检查是否值之一被改变 - 这种改变是一种新的价值
        的for(int i = 0; I< values​​.Length;我++)
            如果(值[I]!= _old [I])
            {
                _old = values​​.ToArray();
                返回值[I];
            }
        返回值[0];
    }    //复制当前值
    公用对象[] ConvertBack(对象的值,类型[] targetTypes,对象参数,CultureInfo的文化)=>
        Enumerable.Repeat(值,targetTypes.Length).ToArray();
}


解决方案

您可以简单地使用一个MultiBinding有一个转换器来实现你想要的逻辑。

 < Popup.IsOpen>
    < MultiBinding转换器={StaticResource的openLogicConverter}>
        <绑定路径=MyAttachedProperty... />
        <绑定路径=IsPopupOpened/>
    < / MultiBinding>
< /Popup.IsOpen>

我通常把这个逻辑视图模型,但它是一个AttachedProperty,有事直接在浏览似乎更适合我。

Is it possible to bind the same property of the control more than once?

To example:

<Popup IsOpen="{Binding Path=(local:ListViewBehavior.IsColumnHeaderClicked),
    RelativeSource={RelativeSource FindAncestor,  AncestorType=GridViewColumnHeader}}" ...

As you can see Popup.IsOpen is bound to attached property. I'd like to bind it to ViewModel IsPopupOpened, but have no idea how.


Trying @Arhiman answer without much success:

<Popup.IsOpen>
    <MultiBinding Converter="{local:MultiBindingConverter}">
        <Binding Path="(local:ListViewBehavior.IsColumnHeaderClicked)"
                 RelativeSource="{RelativeSource FindAncestor, AncestorType=GridViewColumnHeader}" />
        <Binding Path="DataContext.IsPopupId"
                 RelativeSource="{RelativeSource FindAncestor, AncestorType=UserControl}" />
    </MultiBinding>
</Popup.IsOpen>

Naive converter logic:

public class MultiBindingConverter : MarkupExtension, IMultiValueConverter
{
    public MultiBindingConverter() { }

    public override object ProvideValue(IServiceProvider serviceProvider) => this;

    object[] _old;

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (_old == null)
            _old = values.ToArray();
        // check if one of values is changed - that change is a new value
        for (int i = 0; i < values.Length; i++)
            if (values[i] != _old[i])
            {
                _old = values.ToArray();
                return values[i];
            }
        return values[0];
    }

    // replicate current value
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) =>
        Enumerable.Repeat(value, targetTypes.Length).ToArray();
}

解决方案

You could simply use a MultiBinding with a converter to implement the logic you'd like.

<Popup.IsOpen>
    <MultiBinding Converter="{StaticResource openLogicConverter}">
        <Binding Path="MyAttachedProperty" ... />
        <Binding Path="IsPopupOpened" />
    </MultiBinding>
</Popup.IsOpen>

I'd usually put this logic in the ViewModel, but as it is an AttachedProperty, something directly in the View seems more appropriate to me.

这篇关于查看物业 - 两次绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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