views:

134

answers:

3

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));
+3  A: 

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

Muad'Dib
Right. And you can actually turn on the extended tracing for a particular binding only.
Yacoder
Thanks for the diagnostics heads up, that's great, it will come in handy. However, the diagnostics don't tell me much for this problem.The final entry in the diagnostics is ... "TransferValue - using final value 'True'" ... which is what you expect to see, why that doesn't get through to the actual property is still the mystery. How can I setup diagnostics on the actual DP, so I can see what's happening there?
Chris Nicol
you can add a changed event to the DP declaration. check out http://serialseb.blogspot.com/2007/08/wpf-tips-5-receive-notifications-for.html for an example then you can set a breakpoint, etc.
Muad'Dib
Ok ... so the plot thickens ... the DP is getting set, however, it's not executing my code in the DP setter. Am I able to put code in there? Either way, I can use the changed event on the DP to handle that code. Thanks for your help, it got me on back on track.
Chris Nicol
You should not put any extra code in the DP wrapper, because the wrapper Get and Set are not necessarily called at runtime.
emddudley
A: 

That just doesn't make sense =) Should work =)

Did you try to set Mode=TwoWay in the binding? Are you sure you got the DP definition right? Can you add them to the post?

Yacoder
Yes I have tried to set the Mode property, also the UpdateSourceTrigger, in the binding, but nothing helps. I've updated the question
Chris Nicol
Yes, that makes things clearer. Th DP itself is not set by binding! The binding GETs the DP and then changes its VALUE, not the property on the control.It's a common catch, I think I've been there once too =)To get notified on property change, you need a callback (http://msdn.microsoft.com/en-us/library/ms745795.aspx)... Muad'Dib's link is also useful. G'luck =)
Yacoder
Thanks ... I've implemented the solution from Muad'Dib's link and everything is working great now.
Chris Nicol