Pass Data to Data Template Selector
- by Michael Sync
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.