views:

64

answers:

1

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.

A: 

Where did you set the breakpoint in the FollowImageControl constructor? If you had set it before the call to InitializeComponent(), this is normal behavior because InitializeComponent() executes all the XAML. This means, also your binding is created in this method, so that there should be a value after InitializeComponent() has been called, but not before. Of course, AddImg_MouseLeftButtonDown is called after your constructor has completed, and thus InitializeComponent() has also been called.

gehho
I set the breakpoint after the initializecomponent(). It runs the full constructorcode without assigning a value to FollowerId. Thanks for your attention :)
Jakob
I suspect that the FollowImageControl UC contained in Profile is loaded before the profile currentid value is set, and that it's causing the problem, but how then can I pass down the value?
Jakob
it turned out the problem was totally unrelated to this, but in most cases I'm sure your answer would be the correct one, so I'm flagging your answer
Jakob