Is there a way to bind calendar groups dynamicly (through binding) in xaml?
Now it contains a string, but what if you don't know the number of resources upfront.
Or do you have to do it in code behind?
Kind regards,
Erik
Hi Erik,
The CalendarGroups property is not a DependencyProperty so unfortunately it cannot be bound to. You can, however, create a behavior and add a DependencyProperty for CalendarGroups which you can then bind to. Then you can pass the CalendarGroup data from the binding to the XamScheduleDataManager CalendarGroups collection. The behavior might look something like this:
public class CalendarGroupsBinding : Behavior<XamScheduleDataManager> { public IEnumerable<CalendarGroup> CalendarGroups { get { return (IEnumerable<CalendarGroup>)GetValue(CalendarGroupsProperty); } set { SetValue(CalendarGroupsProperty, value); } } // Using a DependencyProperty as the backing store for CalendarGroups. This enables animation, styling, binding, etc... public static readonly DependencyProperty CalendarGroupsProperty = DependencyProperty.Register("CalendarGroups", typeof(IEnumerable<CalendarGroup>), typeof(CalendarGroupsBinding), new PropertyMetadata(null, OnCalendarGroupsPropertyChanged)); private static void OnCalendarGroupsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CalendarGroupsBinding behavior = d as CalendarGroupsBinding; if (behavior != null && behavior.AssociatedObject != null) { behavior.AssociatedObject.CalendarGroups.Clear(); behavior.AssociatedObject.CalendarGroups.InsertRange(0, behavior.CalendarGroups); behavior.SetupEventHandlers(); } } protected override void OnAttached() { base.OnAttached(); } private void SetupEventHandlers() { INotifyCollectionChanged col = this.CalendarGroups as INotifyCollectionChanged; if (col == null) return; col.CollectionChanged += OnCalendarGroupsCollectionChanged; } private void OnCalendarGroupsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { this.AssociatedObject.CalendarGroups.Clear(); this.AssociatedObject.CalendarGroups.InsertRange(0, this.CalendarGroups); } }
Where Behavior<T> is from the System.Windows.Interactivity DLL. You can then attach the behavior to the XamScheduleDataManager like this:
<ig:XamScheduleDataManager x:Name="dataManager" DataConnector="{Binding ElementName=dataConnector}" > <i:Interaction.Behaviors> <local:CalendarGroupsBinding CalendarGroups="{Binding Groups}"/> </i:Interaction.Behaviors>
Let me know if you have any questions on this.