Question

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); }
}
Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top