如果值来自绑定,则DependencyProperty不起作用 [英] DependencyProperty does not work if the value is from a binding

查看:53
本文介绍了如果值来自绑定,则DependencyProperty不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用viewmodel创建了UserControl.它具有DependencyProperty,它仅在直接传递值时才有效.如果该值是通过绑定传递的,则它将不再起作用.

I created UserControl with viewmodel. It has DependencyProperty which only works if the value is passed directly. If the value is passed through the binding, it no longer works.

这是查看代码:

这是一个与其他元素无关的封闭元素.所有列出的物品都属于他.这是一个代码缩短,我不会介绍整个不可估量的结构.

This is a closed element not associated with any other. All listed items belong to him. This is a code shortening, I am not going to present whole, immeasurable structures.

查看

public partial class SomeView : UserControl
{
    public SomeView()
    {
        InitializeComponent();
        SetBinding(ActiveProperty, new Binding(nameof(SomeViewModel.Active)) { Mode = BindingMode.OneWayToSource });
    }

    #region ActiveProperty
    public static readonly DependencyProperty ActiveProperty = DependencyProperty.Register(nameof(Active), typeof(bool), typeof(VNCBoxView));

    public bool Active
    {
        get { return (bool)GetValue(ActiveProperty); }
        set { SetValue(ActiveProperty, value); }
    }
}

VievModel

public class SomeViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool active;

    public bool Active
    {
        get { return active; }
        set
        {
            active = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Active)));
        }
    }
}

UserControl

<UserControl ...>
    <UserControl.DataContext>
        <viewModels:SomeViewModel />
    </UserControl.DataContext>
    <Grid>
        <TextBlock Text="{Binding Active}" />
    </Grid>
</UserControl>

====================================================

===================================================

在使用就绪组件(它是一个单独的实体)时,根据使用方式的不同,会出现此问题.我提醒您,该视图中使用的元素是一个封闭的整体,与使用该视图的元素没有联系.问题的关键在于价值的转移.

When working with a ready component, which is an individual, separate entity, the problem occurs depending on how it is used. I remind you that the elements used in the view in question are a closed whole that does not connect with the element in which it is used. It is the transfer of value that is the matter of the problem.

这是有效的用法:

<local:SomeView Active="True" />

在viewmodel中,setter会被调用两次,一次是false,然后是true.

In viewmodel, the setter is invoked twice, once with false and then with true.

如果该值来自绑定,则无效:

<local:SomeView Active="{Binding SomeParentProperty}" />

在viewmodel中,只会以值false调用一次setter.

In viewmodel, setter is only called once with the value false.

在两种情况下,都不会调用视图中的设置者.

Setters in a view are never called, in both cases.

请帮助

推荐答案

在UserControl的当前DataContext的 SomeViewModel 实例中没有 IsConnected 属性,因此绑定

There is no IsConnected property in the SomeViewModel instance in the current DataContext of the UserControl, hence the Binding

<local:SomeView Active="{Binding IsConnected}" />

不起作用.除非您明确指定其 Source RelativeSource ElementName ,否则它将尝试针对当前DataContext解析PropertyPath.

won't work. It tries to resolve the PropertyPath against the current DataContext, unless you explicitly specify its Source, RelativeSource or ElementName.

这是UserControl绝对不要设置自己的DataContext的确切原因,因此绝不能拥有自己的私有视图模型.

This is the exact reason why UserControls should never explicitly set their own DataContext, and hence never have something like an own, private view model.

UserControl的XAML中的元素不会绑定到此类私有视图模型对象的属性,而是直接绑定到UserControl的属性,例如

The elements in the UserControl's XAML would not bind to properties of such a private view model object, but directly to the properties of the UserControl, for example like

<TextBlock Text="{Binding Active,
    RelativeSource={RelativeSource AncestorType=UserControl}}"/>

这篇关于如果值来自绑定,则DependencyProperty不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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