0

My requirement is to show a custom UserControl as a popup. I am trying to implement this in Silverlight/MVVM using this method. But I am unable to find a way where I can pass some parameter to my popup. I went through this method to achieve it but it's somehow not working.

This is how my XAML looks like:

Behavior to button in View1. On click of this button, I am opening a popup View2:

<cmds:PopupBehavior.CustomUI>
<views:View2 CategoryID="{Binding CategoryID, Mode=TwoWay}"/>
</cmds:PopupBehavior.CustomUI>

CategoryID is a Dependency Property for View2 and is bound to a property of same name in the view model of View1. But for some reason, I always get CategoryID as 0 in View2 even though it's getting set properly in my View1 viewmodel.

Where am I going wrong?

EDIT:

Here is the dependency property code for View2:

public static readonly DependencyProperty CategoryIDProperty = DependencyProperty.Register
("CategoryID",typeof(int),typeof(View2),new PropertyMetadata(0));

public int CategoryID
{
       get { return (int)GetValue(CategoryIDProperty); }
       set { SetValue(CategoryIDProperty, value); }
}

The setter of the property doesn't get called for some reason.

Community
  • 1
  • 1
Vinod
  • 929
  • 1
  • 16
  • 42
  • Please could you include your dependency property code in here as well. I assume you have debugged and made sure that the parameter that is passed to your dp (breakpoint on SetValue in your dp's property) is also 0? – TBohnen.jnr Sep 08 '11 at 05:57
  • I edited the post to include the DP code. And the setter never gets called hence it takes the default value mentioned in the DP registration. – Vinod Sep 08 '11 at 06:02
  • The next step that I can think of is that you have a binding issue, I assume you've looked at your output window to make sure that there are no binding issues and have you checked that your getter is called on the view 1's property? – TBohnen.jnr Sep 08 '11 at 06:09
  • Re: `setter not being called`: Dependency Property setters and getters are there for your use only and are not used by the underlying system to change DP values. Behind the scenes SetValue and GetValue are always used. Just hook up the DP change event handler and that will be called. – iCollect.it Ltd Sep 08 '11 at 09:16

1 Answers1

0

This must be a binding issue on the first view, change your code to the below to test and see if it is:

cmds:PopupBehavior.CustomUI> 
    <views:View2 CategoryID="5"/> 
</cmds:PopupBehavior.CustomUI> 

If your dp's setter now get's called check your output window to see why it can't bind

TBohnen.jnr
  • 5,117
  • 1
  • 19
  • 26
  • As I mention above, DP setters and getters are not used by anything except your own code. Silverlight uses SetValue and GetValue directly. (The exception to this is attached dependency properties as they have static setters which *are* called during XAML parsing). – iCollect.it Ltd Sep 08 '11 at 09:21
  • @TBohnen: I tried setting the value in the XAML. It still doesn't set the value of that property. I put breakpoints on DP registration, DP get and set, Value Changed event of the DP (used this as per HiTechMagic), and constructor of the view. The debug point goes to registration and then directly to the constructor. The setter never gets called :-( – Vinod Sep 08 '11 at 10:57
  • the setter will only be called after the constructor, but then I honestly don't know, something weird is happening. What I suggest is try and recreate a small sample app and see if it does the same thing! – TBohnen.jnr Sep 08 '11 at 12:06
  • Yeah, it is weird. I tried a sample application and hard coding the CategoryID in XAML worked in the recreated sample. However, I still can't get the view1 VM property to bind to this. I feel this is happening because when view1's VM properties are being set, view2 is not even loaded and hence it's DP doesn't get set. But I am not exactly sure how Silverlight control loading works. If someone can throw some light into this it would be great. – Vinod Sep 08 '11 at 12:25