Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to send selected value from datepicker in resource dictionary to dependency property in a WPF?

$
0
0
Hi , I have a resource dictionary  which has dependency property like that:
<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:dp="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestFromDb.Controls"><Style TargetType="{x:Type local:CalendarTimeslotItem}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:CalendarTimeslotItem}"><Border Background="{TemplateBinding Background}"
                            BorderBrush="#A5BFE1"
                            BorderThickness="0,0.5,0,0.5"
                            x:Name="bd"
                            Height="22"><Border CornerRadius="4,4,4,4" BorderThickness="1,1,1,1" BorderBrush="#5D8CC9" x:Name="hover"
                                Opacity="0" Background="#10000000"><TextBlock Text="Click to add appointment" HorizontalAlignment="Center" 
                                       VerticalAlignment="Center" Foreground="#A39DD8" /></Border></Border><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Opacity" Value="1" TargetName="hover" /></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><Style TargetType="{x:Type local:CalendarLedgerItem}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:CalendarLedgerItem}"><Border Background="#E3EFFF"
                            BorderBrush="#6593CF"
                            BorderThickness="0,0,1,1"
                            Height="44" Width="50"><StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"><TextBlock Text="{TemplateBinding TimeslotA}" Foreground="#9493CF" FontSize="16" Margin="0,3,0,0"/><TextBlock Text="{TemplateBinding TimeslotB}" Foreground="#9493CF"  Margin="1.5,0,0,0"/></StackPanel></Border></ControlTemplate></Setter.Value></Setter></Style><Style TargetType="{x:Type local:CalendarDay}"><Setter Property="ItemsPanel"><Setter.Value><ItemsPanelTemplate><local:TimeslotPanel /></ItemsPanelTemplate></Setter.Value></Setter><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:CalendarDay}"><Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"><Grid><StackPanel><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="White" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /><local:CalendarTimeslotItem Background="#E6EDF7" /></StackPanel><ItemsPresenter /></Grid></Border></ControlTemplate></Setter.Value></Setter></Style><!--times--><Style TargetType="{x:Type local:CalendarLedger}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:CalendarLedger}"><Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"><StackPanel><local:CalendarLedgerItem TimeslotA="12" TimeslotB="am" /><local:CalendarLedgerItem TimeslotA="1" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="2" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="3" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="4" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="5" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="6" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="7" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="8" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="9" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="10" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="11" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="12" TimeslotB="pm" /><local:CalendarLedgerItem TimeslotA="1" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="2" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="3" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="4" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="5" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="6" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="7" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="8" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="9" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="10" TimeslotB="00" /><local:CalendarLedgerItem TimeslotA="11" TimeslotB="00" /></StackPanel></Border></ControlTemplate></Setter.Value></Setter></Style><!--end--><Style TargetType="{x:Type local:Calendar}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:Calendar}"><Border Background="#E3EFFF"
                            BorderBrush="#6593CF"
                            BorderThickness="2,2,2,2"><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="50" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="38" /><RowDefinition Height="22" /><RowDefinition Height="*" /></Grid.RowDefinitions><!--next and previous--><StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" 
                                        Margin="5,0,0,0"><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="30"/><ColumnDefinition Width="150"/><ColumnDefinition Width="30"/><ColumnDefinition Width="30"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Button Grid.Column="0" Height="25" Command="{x:Static local:Calendar.PreviousDay}" 
                                            Background="{x:Null}" BorderBrush="{x:Null}" ToolTip="اليوم السابق"><Image Source="/TestFromDb;component/Images/Previous.png" /></Button><DatePicker Grid.Column="1" Name="dpDates" Height="25" Background="{x:Null}"
                                                BorderBrush="{x:Null}" SelectedDate="{Binding Start}"/><Button  Grid.Column="3" Height="25" Command="{x:Static local:Calendar.NextDay}" 
                                             Background="{x:Null}" BorderBrush="{x:Null}" ToolTip="اليوم التالي"><Image Source="/TestFromDb;component/Images/Next.png" /></Button></Grid></StackPanel><!--end--><Border BorderBrush="#6593CF" BorderThickness="0,0,1,1" Grid.Column="0" Grid.Row="1" /><Border BorderBrush="#6593CF" BorderThickness="0,1,0,1" Background="#30000000" Grid.Column="1"
                                    Grid.Row="1"><TextBlock Text="{TemplateBinding CurrentDate}" HorizontalAlignment="Center"
                                           VerticalAlignment="Center" x:Name="dayHeader" /></Border><ScrollViewer Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="50" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions><local:CalendarLedger Grid.Column="0" /><local:CalendarDay Grid.Column="1" x:Name="day" /></Grid></ScrollViewer></Grid></Border></ControlTemplate></Setter.Value></Setter></Style><Style TargetType="{x:Type local:CalendarAppointmentItem}"><Setter Property="StartTime" Value="{Binding StartTime}" /><Setter Property="EndTime" Value="{Binding EndTime}" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:CalendarAppointmentItem}"><Border CornerRadius="4,4,4,4" BorderThickness="1,1,1,1" BorderBrush="#5D8CC9" Background="#F1F5E3" 
                            Margin="1,1,5,1" Padding="3,1.5,0,1.5"><Border.Effect><DropShadowEffect Opacity="0.5" /></Border.Effect><ContentPresenter /></Border></ControlTemplate></Setter.Value></Setter></Style></ResourceDictionary>

    static Calendar()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Calendar), new FrameworkPropertyMetadata(typeof(Calendar)));

            CommandManager.RegisterClassCommandBinding(typeof(Calendar), new CommandBinding(NextDay,
                new ExecutedRoutedEventHandler(OnExecutedNextDay), new CanExecuteRoutedEventHandler(OnCanExecuteNextDay)));

            CommandManager.RegisterClassCommandBinding(typeof(Calendar), new CommandBinding(PreviousDay,
                new ExecutedRoutedEventHandler(OnExecutedPreviousDay), new 
                    CanExecuteRoutedEventHandler(OnCanExecutePreviousDay)));
        }

        #region AddAppointment

        public static readonly RoutedEvent AddAppointmentEvent =
            CalendarTimeslotItem.AddAppointmentEvent.AddOwner(typeof(CalendarDay));

        public event RoutedEventHandler AddAppointment
        {
            add
            {
                AddHandler(AddAppointmentEvent, value);
            }
            remove
            {
                RemoveHandler(AddAppointmentEvent, value);
            }
        }

        #endregion

        #region Appointments

        public static readonly DependencyProperty AppointmentsProperty =
            DependencyProperty.Register("Appointments", typeof(IEnumerable<Appointment>), typeof(Calendar),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(Calendar.OnAppointmentsChanged)));

        public IEnumerable<Appointment> Appointments
        {
            get { return (IEnumerable<Appointment>)GetValue(AppointmentsProperty); }
            set { SetValue(AppointmentsProperty, value); }
        }

        private static void OnAppointmentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Calendar)d).OnAppointmentsChanged(e);
        }

        protected virtual void OnAppointmentsChanged(DependencyPropertyChangedEventArgs e)
        {
            FilterAppointments();
        }

        #endregion

        #region CurrentDate

        /// <summary>
        /// CurrentDate Dependency Property
        /// </summary>
        public static readonly DependencyProperty CurrentDateProperty =
            DependencyProperty.Register("CurrentDate", typeof(DateTime), typeof(Calendar),
                new FrameworkPropertyMetadata((DateTime)DateTime.Now,
                    new PropertyChangedCallback(OnCurrentDateChanged)));

        /// <summary>
        /// Gets or sets the CurrentDate property.  This dependency property 
        /// indicates ....
        /// </summary>
        public DateTime CurrentDate
        {
            get { return (DateTime)GetValue(CurrentDateProperty); }
            set { SetValue(CurrentDateProperty, value); }
        }

        /// <summary>
        /// Handles changes to the CurrentDate property.
        /// </summary>
        private static void OnCurrentDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Calendar)d).OnCurrentDateChanged(e);
        }

        /// <summary>
        /// Provides derived classes an opportunity to handle changes to the CurrentDate property.
        /// </summary>
        protected virtual void OnCurrentDateChanged(DependencyPropertyChangedEventArgs e)
        {
            FilterAppointments();
        }

        private void FilterAppointments()
        {
            DateTime byDate = CurrentDate;
            CalendarDay day = this.GetTemplateChild("day") as CalendarDay;
            day.ItemsSource = Appointments.ByDate(byDate);

            TextBlock dayHeader = this.GetTemplateChild("dayHeader") as TextBlock;
            dayHeader.Text = byDate.DayOfWeek.ToString();
        }
        #endregion

        #region NextDay/PreviousDay

        public static readonly RoutedCommand NextDay = new RoutedCommand("NextDay", typeof(Calendar));
        public static readonly RoutedCommand PreviousDay = new RoutedCommand("PreviousDay", typeof(Calendar));
        // Next Day
        private static void OnCanExecuteNextDay(object sender, CanExecuteRoutedEventArgs e)
        {
            ((Calendar)sender).OnCanExecuteNextDay(e);
        }

        private static void OnExecutedNextDay(object sender, ExecutedRoutedEventArgs e)
        {
            ((Calendar)sender).OnExecutedNextDay(e);
        }

        protected virtual void OnCanExecuteNextDay(CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = false;
        }

        protected virtual void OnExecutedNextDay(ExecutedRoutedEventArgs e)
        {
            CurrentDate += TimeSpan.FromDays(1);
            e.Handled = true;
        }
        // Previous Day
        private static void OnCanExecutePreviousDay(object sender, CanExecuteRoutedEventArgs e)
        {
            ((Calendar)sender).OnCanExecutePreviousDay(e);
        }

        private static void OnExecutedPreviousDay(object sender, ExecutedRoutedEventArgs e)
        {
            ((Calendar)sender).OnExecutedPreviousDay(e);
        }

        protected virtual void OnCanExecutePreviousDay(CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = false;
        }

        protected virtual void OnExecutedPreviousDay(ExecutedRoutedEventArgs e)
        {
            CurrentDate -= TimeSpan.FromDays(1);
            e.Handled = true;
        }

        #endregion

     #region get selected date

        //protected static readonly DependencyProperty StartProperty =
        //     DependencyProperty.Register("Start", typeof(DateTime?), typeof(DatePicker));

        public static readonly DependencyProperty StartProperty =
          DependencyProperty.Register("Start", typeof(DateTime), typeof(DatePicker),
          new FrameworkPropertyMetadata(
                DateTime.Now,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public DateTime Start
        {
            get { return (DateTime)GetValue(DatePicker.SelectedDateProperty);}//GetValue(StartProperty); }
            set { SetValue(StartProperty, value); }
        }

        private static void OnCanExecuteStart(object sender, CanExecuteRoutedEventArgs e)
        {
            ((Calendar)sender).OnCanExecuteStart(e);
        }

        private static void OnExecutedStart(object sender, ExecutedRoutedEventArgs e)
        {
            ((Calendar)sender).OnExecutedStart(e);
        }

        protected virtual void OnCanExecuteStart(CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = false;
        }

        protected virtual void OnExecutedStart(ExecutedRoutedEventArgs e)
        {
            CurrentDate = Start;
            e.Handled = true;
        }
        #endregion
what I want to do is send my selected date from date picker to this dependency property class, but it doesn't work, can anyone help me & till me what I miss please.
thanks.

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>