相关疑难解决方法(0)

使用值绑定分配的依赖项属性不起作用

我有一个带有依赖项属性的usercontrol.

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
}
Run Code Online (Sandbox Code Playgroud)

我在XAML页面中赋值为:

<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" 
                         ExpandCollapse="{Binding PenMenuVisible}" />
Run Code Online (Sandbox Code Playgroud)

但它没有在usercontrol中击中ExpandCollapse属性的GET-SET部分.所以我添加bool到bool转换器只是为了检查带有绑定的值是什么:

<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" 
                         ExpandCollapse="{Binding PenMenuVisible, Converter={StaticResource booleanToBooleanConverter}}" />
Run Code Online (Sandbox Code Playgroud)

在转换器中使用断点,我看到传递的值是正确的.它没有分配给依赖属性的可能原因是什么?

如果我说:在XAML页面中:

<Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" 
                         ExpandCollapse="true"/>
Run Code Online (Sandbox Code Playgroud)

然后它命中usercontrol中的ExpandCollapse属性的GET-SET部分.我被卡住了.这很奇怪.请帮忙.

c# dependency-properties winrt-xaml windows-store-apps windows-8.1

12
推荐指数
2
解决办法
1万
查看次数