R – Problem with Binding Dependency Property on a UserControl

bindingdependency-propertieswpfxaml

I have a two userControls (IconUserControl & DisplayUserControl), I'm having a problem with binding dependency properties, here's some detail:

  • IconUserControl has a bool DP of IsDisplayShown
  • DisplayUserControl has a bool DP of IsDisplayShown

In the XAML I have:

<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.

However, if in the codebehind for the View I set DisplayUserControl.IsDisplayShown = true;, then it works fine.

I have the DataContext for both UserControls set to "this" in the constructor. 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.

What am I doing wrong?

Edit:

Here's the two DPs:

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));

Best Answer

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.

add this namespace to your 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

Related Topic