如何将资源字典中的datepicker中的选定值发送到WPF中的依赖项属性?

时间:2013-06-18 19:27:36

标签: wpf wpf-controls

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

<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>

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

我想要做的是将我选择的日期从日期选择器发送到此依赖属性类,但它不起作用,任何人都可以帮助我&amp;直到我,我想念你。 感谢。

1 个答案:

答案 0 :(得分:0)

我认为在这种情况下应该通过绑定一些值(DateTime,string等)来设置,依赖属性不是变种。在这种情况下,常用DataTemplate和DataContext。例如:

   <!-- In resources -->

   <DataTemplate DataType="{x:Type local:MyDataForCalendar}">

   <!-- Some code -->

       <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}">
                   <Image Source="/TestFromDb;component/Images/Previous.png" />
                </Button>

                <DatePicker Grid.Column="1" Name="dpDates" Height="25" removed="{x:Null}" BorderBrush="{x:Null}" SelectedDate="{Binding DatePickerStart}" />

                <Button  Grid.Column="3" Height="25" Command="{x:Static local:Calendar.NextDay}" Background="{x:Null}" BorderBrush="{x:Null}">
                    <Image Source="/TestFromDb;component/Images/Next.png" />
                </Button>
            </Grid>
        </StackPanel>

    <!-- Some code -->

    </DataTemplate>                 

    <!-- /In resources -->

   <!-- Init your DataTemplate -->
   <Grid>
       <ContentControl Name="CalendarContentControl">
           <local:MyDataForCalendar />
       </ContentControl>
   </Grid>

MyDataForCalendar是一个类,包含为控件设置的所有值。在这种情况下,包含 DatePickerStart

代码:

   public class MyDataForCalendar
   {
      DateTime dateTimeNow = DateTime.Now;

      /// <summary>
      /// Your date
      /// </summary>
      public DateTime DatePickerStart
      {
          get
          {
             return dateTimeNow;
          }

          set
          {
             dateTimeNow = value;
          }
      }
   }