依赖属性在加载时没有值
我的问题是这样的,我有一个名为 profile 的 UC,其中包含另一个名为 FollowImageControl 的 UC。
在我的 Profile.xaml 中,我声明性地将 FollowImageControl 的一个名为 FollowerId 的属性绑定到 Profile.xaml.cs 中的 CurrentUserId。问题是我的 CurrentUserId 是在 Profile.xaml.cs 中分配的; Profile.xaml 代码隐藏。
这意味着我最初并没有获得 FollowerId。我在 FollowImageControl.xaml.cs 中有这些方法:
public static readonly DependencyProperty _followUserId =
DependencyProperty.Register("FollowUserId", typeof(Guid), typeof(FollowImageControl), null);
public Guid FollowUserId
{
get { return (Guid)GetValue(_followUserId); }
set { SetValue(_followUserId, value); }
}
public FollowImageControl()
{
// Required to initialize variables
InitializeComponent();
LoggedInUserId = WebContext.Current.User.UserId;
var ctx = new NotesDomainContext();
if (ctx.IsFollowingUser(LoggedInUserId, FollowUserId).Value) SwitchToDelete.Begin();
}
private void AddImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (LoggedInUserId != FollowUserId)
{
var ctx = new NotesDomainContext();
ctx.FollowUser(FollowUserId, LoggedInUserId);
ctx.SubmitChanges();
}
}
奇怪的事情,当我插入断点时,FollowImageControl() 中的 FollowerUserId 为 0,但它的值是AddImg_MouseLeftButtonDown,并且没有中间逻辑来设置它的值。这是怎么回事???
这是更多代码信息:
这是我来自 profile.xaml 的绑定,
<internalCtrl:FollowImageControl FollowUserId="{Binding ElementName=ProfileCtrl, Path=CurrentUserId}" />
这是我在 profile.xaml.cs 中的构造函数,其中设置了 CurrentUserId public static readonly DependencyProperty _CurrentUserId =
DependencyProperty.Register("CurrentUserId", typeof(Guid), typeof(Profile), null);
public Guid CurrentUserId
{
get { return (Guid)GetValue(_CurrentUserId); }
set { SetValue(_CurrentUserId, value); }
}
public Profile(Guid UserId) {
CurrentUserId = UserId;
InitializeComponent();
Loaded += new RoutedEventHandler(Profile_Loaded);
}
我非常惊讶,前一分钟 FollowerId 还没有值,下一分钟它就持有正确的值,而我却没有更改代码隐藏中的值。
My problem is this, I have a UC called profile that contains another UC called FollowImageControl.
In my Profile.xaml i declaretively bind a property of FollowImageControl called FollowerId to a CurrentUserId from Profile.xaml.cs. Problem is that I CurrentUserId is assigned in Profile.xaml.cs; the Profile.xaml code-behind.
This means that I do not initially get the FollowerId. I have these methods in the FollowImageControl.xaml.cs:
public static readonly DependencyProperty _followUserId =
DependencyProperty.Register("FollowUserId", typeof(Guid), typeof(FollowImageControl), null);
public Guid FollowUserId
{
get { return (Guid)GetValue(_followUserId); }
set { SetValue(_followUserId, value); }
}
public FollowImageControl()
{
// Required to initialize variables
InitializeComponent();
LoggedInUserId = WebContext.Current.User.UserId;
var ctx = new NotesDomainContext();
if (ctx.IsFollowingUser(LoggedInUserId, FollowUserId).Value) SwitchToDelete.Begin();
}
private void AddImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (LoggedInUserId != FollowUserId)
{
var ctx = new NotesDomainContext();
ctx.FollowUser(FollowUserId, LoggedInUserId);
ctx.SubmitChanges();
}
}
THE WEIRD THING IS that when i insert breakpoints the FollowerUserId in FollowImageControl() is 0, but it has a value in AddImg_MouseLeftButtonDown, and there is no inbetween logic that sets the value of it. How is this???
Here's a little more code info:
This is my binding from profile.xaml
<internalCtrl:FollowImageControl FollowUserId="{Binding ElementName=ProfileCtrl, Path=CurrentUserId}" />
this is my constructor in profile.xaml.cs wherein the CurrentUserId is set
public static readonly DependencyProperty _CurrentUserId =
DependencyProperty.Register("CurrentUserId", typeof(Guid), typeof(Profile), null);
public Guid CurrentUserId
{
get { return (Guid)GetValue(_CurrentUserId); }
set { SetValue(_CurrentUserId, value); }
}
public Profile(Guid UserId) {
CurrentUserId = UserId;
InitializeComponent();
Loaded += new RoutedEventHandler(Profile_Loaded);
}
I'm seriously dumbfound that one minute the FollowerId has no value, and the next it holds the right, without me having changed the value in the code-behind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
FollowImageControl
构造函数中的何处设置断点?如果您在调用InitializeComponent()
之前设置了它,这是正常行为,因为InitializeComponent()
会执行所有 XAML。这意味着,您的绑定也是在此方法中创建的,因此在调用InitializeComponent()
之后应该有一个值,而不是之前。当然,AddImg_MouseLeftButtonDown
在构造函数完成后被调用,因此InitializeComponent()
也被调用。Where did you set the breakpoint in the
FollowImageControl
constructor? If you had set it before the call toInitializeComponent()
, this is normal behavior becauseInitializeComponent()
executes all the XAML. This means, also your binding is created in this method, so that there should be a value afterInitializeComponent()
has been called, but not before. Of course,AddImg_MouseLeftButtonDown
is called after your constructor has completed, and thusInitializeComponent()
has also been called.