分配与依赖属性值绑定不起作用 [英] Dependency Property assigned with value binding does not work

查看:135
本文介绍了分配与依赖属性值绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个依赖属性一个用户控件

I have a usercontrol with a dependency property.

public sealed partial class PenMenu : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }         

    public bool ExpandCollapse
    {
        get
        {
            return false;
        }

        set
        {
            //code
        }
    }
public static readonly DependencyProperty ExpandCollapseProperty = DependencyProperty.Register("ExpandCollapse", typeof(bool), typeof(PenMenu), null);
//some more code
}

和我分配在XAML值页面:

And I am assigning value in XAML page as:

<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" 
                         ExpandCollapse="{Binding PenMenuVisible}" />



但它不是打在该用户ExpandCollapse属性GET-SET一部分。
所以我添加布尔为bool转换器只是为了检查正与样结合传递什么样的价值:

But it is not hitting GET-SET part of ExpandCollapse property in the usercontrol. So I added bool to bool converter just to check what value is being passed with binding like:

<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" 
                         ExpandCollapse="{Binding PenMenuVisible, Converter={StaticResource booleanToBooleanConverter}}" />

和与转换器断点,我看到正在传递的值是正确的。
什么是可能的原因,它不是分配给依赖属性。

And with breakpoint in Converter, I see the value being passed is correct. What is the possible reason it's not assigned to the Dependency Property?

在XAML页面另外,如果我说:

Also in XAML page if I say:

<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" 
                         ExpandCollapse="true"/>



然后射在该用户ExpandCollapse属性的GET-SET一部分。
我卡住了。这很奇怪。请帮忙。

then it hits the GET-SET part of ExpandCollapse property in the usercontrol. I am stuck. This is weird. Please help.

推荐答案

这是令人沮丧,不是吗?首先,包括更改的事件处理程序。像这样的:

It's frustrating isn't it? First, include a changed event handler. Like this:

public string Title
{
    get { return (string)GetValue(TitleProperty); }
    set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(string), 
    typeof(MyControl), new PropertyMetadata(string.Empty, Changed));
private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var c = d as MyControl;
    // now, do something
}



然后,请阅读这篇文章所以你看到有更多的陷阱不仅仅是一句:的http:// blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html

祝你好运!

这篇关于分配与依赖属性值绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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