Dave
Dave

Reputation: 8461

Cannot bind to a dependency property

I'm struggling to a bind to a dependency property.

My application has a MainWindow. In this is a usercontrol (called timeline). Within the TimeLine UserControl is another Control called MoveMe.

I can bind to the dependency properties of the timeline UserControl from the MainWindow. I can bind to the MoveMe UserControl from the MoveMe when I use OneWayToSource binding. However, I'm trying to bind from the timeline UserControl to the MoveMe control (from the parent to the child). However, the binding is not working. I put a watch on the setter of a MoveMe property and it's never fired.

There are no bind issues in the Output window.

My MainWindow.xaml has

<timeline:TimeLine StartTime="{Binding StartTime}" 
                                       EndTime="{Binding EndTime}" 
                                       TestEvents="{Binding TestEvents}" 
                                       CampaignDetails="{Binding CampaignDetails}" 
                                       ShowReportPeriod="True" HorizontalAlignment="Stretch"/>    

My timeline.xaml has

<userControls:MoveMe 
        StartMarkerPositionPixels="{Binding RelativeSource={RelativeSource AncestorType=userControls:TimeLine}, Path=StartMarkerPosition, Mode=OneWayToSource}"               
        EndReportPositionInPixels="{Binding RelativeSource={RelativeSource AncestorType=userControls:TimeLine}, Path=EndOfReportPosition, Mode=OneWay}" 
        x:Name="ReportSelectorStart" />

and it's DataContext is set like

<UserControl DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=Window}}"...

So, as you can see above, in the timeline.xaml there are 2 properties being bound, the first being StartMarkerPositionPixels which is OneWayToSource. This works fine. The issue is the second, EndReportPositionInPixels as this does not bind.

The code behind of the MoveMe control

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace TimeLineCanvas.UserControls
{
    /// <summary>
    /// Interaction logic for MoveMe.xaml
    /// </summary>
    public partial class MoveMe : UserControl
    {
        public MoveMe()
        {
            InitializeComponent();            
            //this.DataContext = this;
        }

        public double StartMarkerPositionPixels
        {
            get { return (double)GetValue(StartMarkerPositionProperty); }
            set { SetValue(StartMarkerPositionProperty, value); }
        }

        public double EndReportPositionInPixels
        {
            get { return (double)GetValue(ScaleFactorProperty); }
            set { SetValue(ScaleFactorProperty, value);
            OnPropertyChanged("EndReportPositionInPixels");
            }
        }

        public static readonly DependencyProperty EndMarkerPositionProperty = DependencyProperty.Register(
            "EndMarkerPositionPixels",
            typeof(double),
            typeof(MoveMe));

        public static readonly DependencyProperty EndReportPositionInPixelsPoperty = DependencyProperty.Register(
          "EndReportPositionInPixels",
          typeof(double),
          typeof(MoveMe));

    }
}

In my TimeLine codebehind, I have the following

    private double _endOfReportPosition;
    public double EndOfReportPosition
    {
        get { return _endOfReportPosition; }
        set
        {
            _endOfReportPosition = value;
            //this.ReportSelectorStart.EndMarkerPositionPixels = value;//If I hardcode it in, then it works, but I want it bind
            OnPropertyChanged("EndOfReportPosition");
        }
    }

The output window confirms no binding errors.

My question is, how do I pass a value from my TimeLine control to my MoveMe control via a dependency property.

Upvotes: 0

Views: 308

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81313

First of all you don't need INPC for dependency properties. Second look at this code -

    public double EndReportPositionInPixels
    {
        get { return (double)GetValue(ScaleFactorProperty); }
        set { SetValue(ScaleFactorProperty, value); }
    }

You are setting and getting values for ScaleFactorProperty here. Shouldn't it be EndReportPositionInPixelsPoperty like this -

    public double EndReportPositionInPixels
    {
        get { return (double)GetValue(EndReportPositionInPixelsPoperty); }
        set { SetValue(EndReportPositionInPixelsPoperty, value); }
    }

Upvotes: 2

Related Questions