Dependencyproperty doesn't have value on load
- by Jakob
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.