/// <summary> /// Raises the TemplateRequested event up the 'container' element's logical tree /// so that the DataTemplate to return can be determined. /// </summary> /// <param name="item">The data object being templated.</param> /// <param name="container">The element which contains the data object.</param> /// <returns>The DataTemplate to apply.</returns> public override DataTemplate SelectTemplate(object item, DependencyObject container) { // We need 'container' to be a UIElement because that class // exposes the RaiseEvent method. UIElement templatedElement = container as UIElement; if (templatedElement == null) throw new ArgumentException("RoutedDataTemplateSelector only works with UIElements."); // Bubble the TemplateRequested event up the logical tree, starting at the templated element. // This allows the outside world to determine what template to use. TemplateRequestedEventArgs args = new TemplateRequestedEventArgs(TemplateRequestedEvent, templatedElement, item); templatedElement.RaiseEvent(args); // Return the DataTemplate selected by the outside world. return args.TemplateToUse; }
/// <summary> /// Raises the TemplateRequested event up the 'container' element's logical tree /// so that the DataTemplate to return can be determined. /// </summary> /// <param name="item">The data object being templated.</param> /// <param name="container">The element which contains the data object.</param> /// <returns>The DataTemplate to apply.</returns> public override DataTemplate SelectTemplate(object item, DependencyObject container) { // We need 'container' to be a UIElement because that class // exposes the RaiseEvent method. UIElement templatedElement = container as UIElement; if (templatedElement == null) { throw new ArgumentException("RoutedDataTemplateSelector only works with UIElements."); } // Bubble the TemplateRequested event up the logical tree, starting at the templated element. // This allows the outside world to determine what template to use. TemplateRequestedEventArgs args = new TemplateRequestedEventArgs(TemplateRequestedEvent, templatedElement, item); templatedElement.RaiseEvent(args); // Return the DataTemplate selected by the outside world. return(args.TemplateToUse); }