我有一个包含用户ListView的页面:
<ListView ItemsSource="{Binding UserList}"
SelectedItem="{Binding SelectedUser}">
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Id}" />
<GridViewColumn DisplayMemberBinding="{Binding Name}"/>
</GridVie>
</ListView>
我还有一个用户控件,它将显示该ListView中所选用户的所有数据。因此,我将SelectedUser
绑定到CurrentUser
UserUc
<userControls:UserUc CurrentUser="{Binding SelectedUser}" />
用户控件不显示数据!为什么?
Edit
我将此添加到页面后面的代码中:
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");
}
当我更改任何用户的选择时,设置者被解雇
Second edit
:用户控件
<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); }
}
答案 0 :(得分:1)
如果你将usercontrol中的datacontext设置为self - 那就错了。 请使用ElementName绑定。所以你要做的就是删除你的DataContext =“{Binding RelativeSource = {RelativeSource Self}}”
<UserControl x:Name="uc">
<ListView ItemsSource="{Binding ElementName=uc,CurrentUser.CommentList}">
<!-- I want to show the user comments -->
</ListView>