none
WPF MVVM - Dependency Property Binding in UserControl RRS feed

  • Question

  • Hi,
    My solution is implemented in MVVM. The view is a window which hosts a usercontrol. I have created a dependency property for this userControl as below :
            public static DependencyProperty ListProperty = DependencyProperty.Register(
                  "ItemsList", typeof(List<RequiredClass>), typeof(UsercontrolTest));
            public List<RequiredClass> ItemsList
            {
                get
                {
                    return (List<RequiredClass>)GetValue(ListProperty);
                }
                set
                {
                    SetValue(ListProperty, value);
                }
            }
    This property is bound to my viewmodel property (ListOfItems) in xaml :
          <Window x:Class="TestProject.MainWindow"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:Test="clr-namespace:TestProject"
                Title="MainWindow" Height="350" Width="525">
                 <Grid>
                   <Grid.RowDefinitions>
                     <RowDefinition></RowDefinition>             
                     <RowDefinition></RowDefinition>
                   </Grid.RowDefinitions>
                   <Test:UserControlTest Grid.Row="0" ItemsList="{Binding Path=ListOfItems}" >  </Test:UserControlTest>
                   <Button Grid.Row="1" Content="AddItems" Click="Button_Click"></Button>
               </Grid>
          </Window>
    Also i have intialized the datacontext of the window in codebehind to the viewmodel. Problem is the binding never seems to happen and the set property is never called for the dependency property. Am i missing something here ?

    Aabid Ali
    Sunday, February 5, 2012 5:49 AM

Answers

  • Hi , Thanks for the answer .Forgive me regarding the mismatch in names . Was necessary to rename it :) and did it in a hurry .

     

    Coming back to my question : 

    Debugger didnt enter the setter and i was assuming it wasnt binding. I really needed some code to be executed on change of dependency property .

    Solved that problem now using Property Metadata. DP is really awesome once you get to know the kinks :).

    Nevertheless thanks for your tips regarding DP. will keep em in mind.


    Aabid Ali
    Sunday, February 5, 2012 12:29 PM

All replies

  • The first thing I notice is in the dp code you reference "UsercontrolTest" and in the XAML you reference a different/wrong object "UserControlTest".

    But I presume that was an error in posting, as it wouldn't compile otherwise.

    Next, you don't show all the binding (ListOfItems), I don't see an x:name for code behind or please post the ViewModel that the datacontext is bound to.

     

    With most binding problems, first thing to do is check the Output tab of Visual Studio when run, to see if there are any binding errors listed.

     

    Can you confirm there are no binding errors?

    If none, please post the rest of the code so we can see the properties that are bound, and reproduce in our own test project.

     

    Regards,
    Pedro


    If you find my post helpful, please remember to "Mark As Answer" and/or "Vote as Helpful"

    Sunday, February 5, 2012 10:45 AM
    Moderator
  • Another thing I've just noticed, your dependancy property has to follow an exact naming convention. If it is "ItemsList", then you must use "ItemsListProperty", but you've just got "ListProperty". I'm guessing that's the actual problem. I have tested in my own test project and that works fine. If you are still struggling, I'll post the whole test project here.
     
    So, your code should be:
     
            public static DependencyProperty ItemsListProperty = DependencyProperty.Register("ItemsList", typeof(List<object>), typeof(UserControlTest));
            public List<object> ItemsList
            {
                get
                {
                    return (List<object>)GetValue(ItemsListProperty);
                }
                set
                {
                    SetValue(ItemsListProperty, value);
                }
            }
    Best regards,
    Pedro

    If you find my post helpful, please remember to "Mark As Answer" and/or "Vote as Helpful"

    Sunday, February 5, 2012 10:55 AM
    Moderator
  • Hi , Thanks for the answer .Forgive me regarding the mismatch in names . Was necessary to rename it :) and did it in a hurry .

     

    Coming back to my question : 

    Debugger didnt enter the setter and i was assuming it wasnt binding. I really needed some code to be executed on change of dependency property .

    Solved that problem now using Property Metadata. DP is really awesome once you get to know the kinks :).

    Nevertheless thanks for your tips regarding DP. will keep em in mind.


    Aabid Ali
    Sunday, February 5, 2012 12:29 PM
  • Hi Aabid,

    It is greate to see you have resolve your issue.

    But also for using DependencyProperty, I didn't see UserControlTest XAML code here, do you have set binding with itemsSource DP and the ListControl in UserControl? Please check out below link for your reference:

    http://social.msdn.microsoft.com/Forums/bs-Latn-BA/wpf/thread/b9c27238-145b-494f-8d22-8fa1bda336ad 

    If you have any additional questions, please feel free to let me know.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 7, 2012 3:36 AM
    Moderator