如何将资源字典中的datepicker中的选定值发送到WPF中的依赖项属性? [英] How to send selected value from datepicker in resource dictionary to dependency property in a WPF?

查看:65
本文介绍了如何将资源字典中的datepicker中的选定值发送到WPF中的依赖项属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资源字典,其中包含依赖属性:



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 removed="{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" removed="#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 removed="#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 removed="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <StackPanel>
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="White" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#E6EDF7" />
                                <local:CalendarTimeslotItem removed="#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 removed="{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 removed="#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" removed="{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" removed="#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" removed="#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.



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.

推荐答案

Your first problem is that you have nothing to bind to. What you should be doing is using ListBox instead of StackPanel. Then you can Bind to the SelectedItem property. If you do not like the container for the default ListBox, then you override the ItemsPanel, and you can also override the default ItemTemplate:

Your first problem is that you have nothing to bind to. What you should be doing is using ListBox instead of StackPanel. Then you can Bind to the SelectedItem property. If you do not like the container for the default ListBox, then you override the ItemsPanel, and you can also override the default ItemTemplate:
<listbox name="ListBoxAvailablePickedBrokers">
	ItemsSource="{Binding AvailablePickedBrokers}"
	BorderThickness="0"
	Background="Transparent"
	Margin="-15,15,-15,0"
	MaxHeight="168">
	<listbox.itemspanel>
		<itemspaneltemplate>
			<wrappanel orientation="Horizontal">
			 HorizontalAlignment="Stretch"
			 Width="480"
			 Background="Transparent" />
		</wrappanel></itemspaneltemplate>
	</listbox.itemspanel>
	<listbox.itemtemplate>
		<datatemplate>
			<button content="{Binding BrokerId}">
					ToolTip="{Binding BrokerName}"
					Width="39"
		Command="{Binding DataContext.PickedBrokerSelection, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
				CommandParameter="{Binding BrokerId}"						Click="PickedBrokerButton_OnClick"
				Margin="-.75,.25" />
		</button></datatemplate>
	</listbox.itemtemplate>
</listbox>





This is just a template to get you started.



This is just a template to get you started.


这篇关于如何将资源字典中的datepicker中的选定值发送到WPF中的依赖项属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆