TimeIndicator ScheduleView Binding

2 Answers 52 Views
ScheduleView
Martin
Top achievements
Rank 1
Iron
Martin asked on 01 Dec 2022, 12:33 PM

How do I bind the DateTime Property (return value for GetDateTime())

of a class implementing ITimeIndicator to a Property of my ViewModel?

 

my thoughts:


         <telerik:RadScheduleView.TimeIndicatorsCollection>
            <telerik:TimeIndicatorsCollection>
               <local:CustomTimeIndicator Location="WholeArea" Now="{Binding CurrentNow}" />
            </telerik:TimeIndicatorsCollection>
         </telerik:RadScheduleView.TimeIndicatorsCollection>



public class CustomTimeIndicator : DependencyObject, ITimeIndicator
{
   public static readonly DependencyProperty NowProperty = DependencyProperty.RegisterAttached("Now", typeof(DateTime), typeof(CustomTimeIndicator));

   public DateTime Now
   {
      get => (DateTime)GetValue(NowProperty);
      set => SetValue(NowProperty, value);
   }

   public DateTime GetDateTime()
   {
      return Now;
   }

   public TimeSpan Offset { get; set; }

   public CurrentTimeIndicatorLocation Location { get; set; }
}

Gives me:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=CurrentNow; DataItem=null; target element is 'CustomTimeIndicator' (HashCode=47896050); target property is 'Now' (type 'DateTime')

 

2 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 06 Dec 2022, 10:48 AM

Hello Martin,

The binding doesn't work because the time indicators are not visual elements, thus they don't inherit the DataContext from their logical parents. To achieve your requirement, you should assign the data context of the time indicator explicitly. One way to do so is to define the view model as a resource and use the Source of the binding. For example:

<UserControl.Resources>
	<local:ViewModel x:Key="ViewModel"/>
</UserControl.Resources>

<Grid DataContext="{StaticResource ViewModel}">	
	<telerik:RadScheduleView> <!-- some other settings here -->
		<telerik:RadScheduleView.TimeIndicatorsCollection>
			<telerik:TimeIndicatorsCollection>
				<local:CustomTimeIndicator Location="WholeArea" Now="{Binding Source={StaticResource ViewModel}, Path=CurrentNow}" />
			</telerik:TimeIndicatorsCollection>
		</telerik:RadScheduleView.TimeIndicatorsCollection>
		<!-- more settings here -->
	</telerik:RadScheduleView>
	</Grid>
</UserControl>

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Martin
Top achievements
Rank 1
Iron
answered on 09 Dec 2022, 04:34 PM
Thanks, that helpd
Tags
ScheduleView
Asked by
Martin
Top achievements
Rank 1
Iron
Answers by
Martin Ivanov
Telerik team
Martin
Top achievements
Rank 1
Iron
Share this question
or