4

How do you guys pass the data (parameter) to DataTemplate Selector?

The only one that I can think of is to use an attached property in DataTemplate Selector?

Example:

public class DisableWeekendsSelection : DataTemplateSelector
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible", Justification = "DependencyProperty")]
        public static readonly DependencyProperty Parameter =
           DependencyProperty.RegisterAttached("Parameter", typeof(ObservableCollection<Date>), typeof(DisableWeekendsSelection),
           new FrameworkPropertyMetadata(null,
               FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public static ObservableCollection<Date> GetParameter(DependencyObject dp)
        {
            return dp.GetValue(Parameter) as ObservableCollection<Date>;            
        }

        public static void SetParameter(DependencyObject dp, ObservableCollection<Date> value)
        {            
            dp.SetValue(Parameter, value);
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {

The problem with this approach is that I'm not able to get the value of Parameter in SelectTemplate method.

Any suggestion would be appreciated. Thanks in advance.

Michael Sync
  • 4,834
  • 10
  • 40
  • 58

3 Answers3

4

This is quite a bit easier. In the handler:

SelectTemplate(object item, DependencyObject container)

The container is in the logical tree. You can recurse up the tree to get to your UserControl and you then have access to that controls properties and DataContext. Like this:

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
    var _Container = Library.TreeHelper.TryFindParent<MyUserControl>(container);

Here's the helper method: http://www.hardcodet.net/2009/06/finding-elements-in-wpf-tree-both-ways

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
0

The usual solution is to use the properties of the item parameter to get the needed info to choose the right template.

If you have a ViewModel, it can include the necessary data.

Timores
  • 14,439
  • 3
  • 46
  • 46
  • I have Date property and Holidays Property. DatePicker is binded with Date. I need to highlight holidays in Calender of DatePicker. That's why Im trying to pass the Holidays of VM to Data Template Selector. – Michael Sync Mar 17 '10 at 00:09
  • I always get null if I put this line var dates = container.GetValue(Parameter) as ObservableCollection; in SelectTemplate. :( – Michael Sync Mar 17 '10 at 07:11
  • DayTemplateSelector="{StaticResource disableHolidaySelection}" in telerik:RadDatePicker.. we have and in resource.. Example: http://www.telerik.com/community/forums/wpf/calendar/selectors-and-special-dates.aspx#1127883 – Michael Sync Mar 17 '10 at 09:14
  • I managed to make it work by using an attached property and static variable.. but i dont think it's not good solution to use static variable so i'm seeking the suggestion from you guys.. – Michael Sync Mar 17 '10 at 09:28
  • Sorry, I installed the RadControls and tried to find a solution, but could not succeed. – Timores Mar 17 '10 at 21:00
0

Workarounds

  1. Use the attached property and private static variable

  2. Walk the Visual Tree doc.OfParentType<>

Michael Sync
  • 4,834
  • 10
  • 40
  • 58