Pass Data to Data Template Selector

Posted by Michael Sync on Stack Overflow See other posts from Stack Overflow or by Michael Sync
Published on 2010-03-16T16:38:06Z Indexed on 2010/03/16 16:41 UTC
Read the original article Hit count: 462

Filed under:

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.

© Stack Overflow or respective owner

Related posts about wpf