PageRenderTime 43ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ReactiveUI.Routing/AutoDataTemplateBindingHook.cs

https://github.com/bsiegel/ReactiveUI
C# | 58 lines | 50 code | 8 blank | 0 comment | 9 complexity | 03eb09dc1529f25f2b37231217583d09 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0, LGPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. #if WINRT
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9. using Windows.UI.Xaml.Markup;
  10. #else
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Markup;
  14. #endif
  15. namespace ReactiveUI.Routing
  16. {
  17. public class AutoDataTemplateBindingHook : IPropertyBindingHook
  18. {
  19. public static DataTemplate DefaultItemTemplate = (DataTemplate)
  20. #if SILVERLIGHT || WINRT
  21. XamlReader.Load(
  22. #else
  23. XamlReader.Parse(
  24. #endif
  25. #if WINRT
  26. "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:routing='using:ReactiveUI.Routing'>" +
  27. "<routing:ViewModelViewHost ViewModel=\"{Binding}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" IsTabStop=\"False\" />" +
  28. "</DataTemplate>"
  29. #else
  30. "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
  31. "xmlns:routing='clr-namespace:ReactiveUI.Routing;assembly=ReactiveUI.Routing'> " +
  32. "<routing:ViewModelViewHost ViewModel=\"{Binding}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" IsTabStop=\"False\" />" +
  33. "</DataTemplate>"
  34. #endif
  35. );
  36. public bool ExecuteHook(object source, object target, Func<IObservedChange<object, object>[]> getCurrentViewModelProperties, Func<IObservedChange<object, object>[]> getCurrentViewProperties, BindingDirection direction)
  37. {
  38. var viewProperties = getCurrentViewProperties();
  39. var itemsControl = viewProperties.Last().Sender as ItemsControl;
  40. if (itemsControl == null) return true;
  41. if (viewProperties.Last().PropertyName != "ItemsSource") return true;
  42. if (itemsControl.ItemTemplate != null) return true;
  43. #if !SILVERLIGHT
  44. if (itemsControl.ItemTemplateSelector != null) return true;
  45. #endif
  46. itemsControl.ItemTemplate = DefaultItemTemplate;
  47. return true;
  48. }
  49. }
  50. }