简体   繁体   English

UserControl上绑定依赖属性的问题

[英]Problem with Binding Dependency Property on a UserControl

I have a two userControls (IconUserControl & DisplayUserControl), I'm having a problem with binding dependency properties, here's some detail: 我有两个userControls(IconUserControl和DisplayUserControl),我遇到了绑定依赖项属性的问题,这里有一些细节:

  • IconUserControl has a bool DP of IsDisplayShown IconUserControl有一个IsDisplayShown的bool DP
  • DisplayUserControl has a bool DP of IsDisplayShown DisplayUserControl具有IsDisplayShown的bool DP

In the XAML I have: 在XAML中我有:

<local:DisplayUserControl
                    x:Name="DisplayUserControl"
                    IsDisplayShown="{Binding ElementName=IconUserControl, Path=IsDisplayShown, Converter={StaticResource DummyConverter}}" />

<local:IconUserControl
                    x:Name="IconUserControl" />

When IconUserControl.IsDisplayShown is set to true, I can see in the DummyConverter this value getting passed, but it never sets DisplayUserControl.IsDisplayShown. 当IconUserControl.IsDisplayShown设置为true时,我可以在DummyConverter中看到这个值被传递,但它从不设置DisplayUserControl.IsDisplayShown。

However, if in the codebehind for the View I set DisplayUserControl.IsDisplayShown = true; 但是,如果在View的后台设置我设置DisplayUserControl.IsDisplayShown = true; , then it works fine. ,那它工作正常。

I have the DataContext for both UserControls set to "this" in the constructor. 我有两个UserControls的DataContext在构造函数中设置为“this”。 I've tried to fiddle with the "Mode" property of the binding. 我试图摆弄绑定的“模式”属性。

*Note: DummyConverter just returns the value, I only have this to confirm that the Binding is trying to work. *注意:DummyConverter只返回值,我只有这个确认Binding正在尝试工作。

What am I doing wrong? 我究竟做错了什么?

Edit: 编辑:

Here's the two DPs: 这是两个DP:

public bool IsDisplayShown
        {
            get { return (bool)GetValue(IsDisplayShownProperty); }
            set { SetValue(IsDisplayShownProperty, value); }
        }
        public static readonly DependencyProperty IsDisplayShownProperty =
            DependencyProperty.Register("IsDisplayShown", typeof(bool), typeof(IconUserControl), new UIPropertyMetadata(false));

public bool IsDisplayShown
        {
            get { return (bool)GetValue(IsDisplayShownProperty); }
            set
            {
                if (value)
                    ShowOpenItems();
                else
                    HideOpenItems();
                SetValue(IsDisplayShownProperty, value);
            }
        }
        public static readonly DependencyProperty IsDisplayShownProperty=
            DependencyProperty.Register("IsDisplayShown", typeof(bool), typeof(DisplayUserControl), new UIPropertyMetadata(false));

This should help you, but probably won't solve the whole problem. 这应该对你有帮助,但可能无法解决整个问题。 It is a good place to start, though. 不过,这是一个很好的起点。 Adding this code will cause debugging info for the binding to dump to your Debug window in Visual Studio. 添加此代码将导致绑定的调试信息转储到Visual Studio中的“调试”窗口。

add this namespace to your xaml.... 将此命名空间添加到您的xaml ....

xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"

then, your binding, add this: 然后,你的绑定,添加这个:

diagnostics:PresentationTraceSources.TraceLevel=High

check Bea Stollnitz article for more information 查看Bea Stollnitz文章了解更多信息

That just doesn't make sense =) Should work =) 那只是没有意义=)应该工作=)

Did you try to set Mode=TwoWay in the binding? 您是否尝试在绑定中设置Mode = TwoWay? Are you sure you got the DP definition right? 你确定你的DP定义合适吗? Can you add them to the post? 你能把它们添加到帖子中吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM