1

I have a Page which contains a ListView of users :

<ListView ItemsSource="{Binding UserList}"
          SelectedItem="{Binding SelectedUser}">
    <GridView>
        <GridViewColumn DisplayMemberBinding="{Binding Id}" />
        <GridViewColumn DisplayMemberBinding="{Binding Name}"/>
    </GridVie>
</ListView>

I have also a user control which will display all the data of the selected user from that ListView. So I binded the SelectedUser to a dependency property CurrentUser of UserUc

<userControls:UserUc CurrentUser="{Binding SelectedUser}" />

The user control does not display the data ! Why ?

Edit

I added this to the code behind of the page :

public static readonly DependencyProperty SelectedUserProperty = WpfDpHelper.Register<UserListUc, User>(i => i.SelectedUser, Callback);

private static void Callback(UserListUc userListUc, DependencyPropertyChangedEventArgs<User> DependencyPropertyChangedEventArgs)
{
    Debug.WriteLine("user selection changed");
}

And when I change the selection of any user the setter gets fired

Second edit : The user control

<UserControl ...
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <ListView ItemsSource="{Binding CurrentUser.CommentList}">
        <!-- I want to show the user comments -->
    </ListView>
</UserControl>

Code behind

public static readonly DependencyProperty CurrentUserProperty = WpfDpHelper.Register<UserUc, User>(i => i.CurrentUser);
public User CurrentUser
{
    get { return (User) GetValue(CurrentUserProperty); }
    set { SetValue(CurrentUserProperty, value); }
}
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174

1 Answers1

1

if you set the datacontext in your usercontrol to self - thats wrong. pls use ElementName Binding. So all you have to do is to remove your DataContext="{Binding RelativeSource={RelativeSource Self}}"

 <UserControl x:Name="uc">
   <ListView ItemsSource="{Binding ElementName=uc,CurrentUser.CommentList}">
    <!-- I want to show the user comments -->
   </ListView>
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • You were right sir. I had to remove the DataContrext in the User control. But I don't understand why I can't use the DataContext in both of them ? – Wassim AZIRAR Dec 06 '13 at 14:30
  • 1
    check the comments from H.B. here: http://stackoverflow.com/questions/11226843/data-binding-in-wpf-user-controls thats a good explanation :) – blindmeis Dec 06 '13 at 14:33