Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an example of using best practice with the sort of control I think you are attempting to build (see notes at end for some explanations):-</p> <pre><code>[TemplatePart(Name = DatePicker.ElementStartDate, Type = typeof(RadDatePicker))] [TemplatePart(Name = DatePicker.ElementEndDate, Type = typeof(RadDatePicker))] public class DatePicker : Control { public DatePicker() { this.DefaultStyleKey = typeof(DatePicker); } #region Template Part Names private const string ElementStartDate = "startDate"; private const string ElementEndDate = "endDate"; #endregion #region Template Parts private RadDatePicker _StartDate; internal RadDatePicker StartDate { get { return _StartDate; } private set { if (_StartDate != null) { _StartDate.SelectionChanged -= StartDate_SelectionChanged; } _StartDate = value; if (_StartDate != null) { _StartDate.SelectionChanged += StartDate_SelectionChanged; } } } private RadDatePicker _EndDate; internal RadDatePicker EndDate { get { return _EndDate; } private set { if (_EndDate!= null) { _EndDate.SelectionChanged -= EndDate_SelectionChanged; } _EndDate= value; if (_EndDate!= null) { _EndDate.SelectionChanged += EndDate_SelectionChanged; } } } #endregion public static readonly DependencyProperty StartDateSelectedDateProperty = DependencyProperty.Register( "StartDateSelectedDateProperty", typeof(DateTime?), typeof(DatePicker), new PropertyMetaData(new DateTime(2010, 01, 01))); public DateTime? StartDateSelectedDate { get { return (DateTime?)GetValue(StartDateSelectedDateProperty); } set { SetValue(StartDateSelectedDateProperty)} } public static readonly DependencyProperty EndDateSelectedDateProperty = DependencyProperty.Register( "EndDateSelectedDateProperty", typeof(DateTime?), typeof(DatePicker), new PropertyMetaData(new DateTime(2010, 01, 01))); public DateTime? EndDateSelectedDate { get { return (DateTime?)GetValue(EndDateSelectedDateProperty); } set { SetValue(EndDateSelectedDateProperty)} } public override void OnApplyTemplate() { base.OnApplyTemplate(); StartDate = GetTemplateChild(ElementStartDate) as RadDatePicker; EndDate = GetTemplateChild(ElementEndDate) as RadDatePicker; } void StartDate_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e) { // Do stuff with StartDate here } void EndDate_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e) { // Do stuff with EndDate here } } </code></pre> <p>The template Xaml should look like:-</p> <pre><code>&lt;Style TargetType="local:DatePicker"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="local:DatePicker"&gt; &lt;Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" x:Name="myDatePickerContentArea"&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;Button x:Name="myTestButton" Content="Test button" /&gt; &lt;telerik:RadDatePicker x:Name="startDate" Style="{StaticResource VisitsReportTextBoxStyle}" Foreground="#FFFFFF" DateTimeWatermarkContent="Start Date" SelectedDate="{TemplateBinding StartDateSelectedDate}" /&gt; &lt;telerik:RadDatePicker x:Name="endDate" Style="{StaticResource VisitsReportTextBoxStyle}" DateTimeWatermarkContent="End Date" SelectedDate="{TemplateBinding EndDateSelectedDate}" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p><strong>Some Explanations</strong></p> <ul> <li>A key problem your original code had was that it hadn't implemented the dependency properties correctly. Note that properties now use <code>GetValue</code> and <code>SetValue</code>, also that the property meta data is used to assign the default rather than attempting to set in in onapplytemplate.</li> <li>With the properties correctly implemented the template binding should work and in fact we're done as far as getting what appears to be your original intent, hence I've left out any actual code in the event handlers.</li> <li>Create constants in the code to hold the names of key template parts that you want to interact with, this allows for name changes to be less expensive to make.</li> <li>Add <code>TemplatePart</code> attributes to the class to indicate the key elements that the code expects to find, what their names should be and what base type they are expected to have. This allows for a designer to re-template an existing control, as long the declared template parts are present somewher the control should function correctly even if its UI is radically altered.</li> <li>If you need to attach event handlers for some elements create a field to hold a reference to the element and then create a property to wrap round it. The property setter should then detach and attach the event handlers as you see in the code.</li> <li>Make sure bae.OnApplyTemplate is called in the override of OnApplyTemplate then as you can see its quite straight forward to assign the above created properties.</li> <li>I don't have the RadDatePicker so I can't test, my only outstanding concern is where the <code>DateTime?</code> is the correct type for the SelectedDate property. Certainly if it is its an improvement over the Microsoft offering which seems to have drop the ball on this typical data entry requirement.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload