Guest User

Untitled

a guest
Jan 11th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. class Shelf : Panel
  2. {
  3. public static readonly DependencyProperty ExactProperty = DependencyProperty.RegisterAttached("Exact", typeof(int), typeof(Shelf),
  4. new FrameworkPropertyMetadata(100, new PropertyChangedCallback(OnExactChanged)));
  5. private static void OnExactChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  6. {
  7. Console.WriteLine(d); // This Callback is added only for debug purposes
  8. }
  9. public static int GetExact(UIElement element)
  10. {
  11. return (int)element.GetValue(ExactProperty);
  12. }
  13. public static void SetExact(UIElement element, int value)
  14. {
  15. element.SetValue(ExactProperty, value);
  16. }
  17. protected override Size MeasureOverride(Size availableSize)
  18. {
  19. Size panelSize = new Size(availableSize.Width, 0);
  20. foreach (UIElement child in InternalChildren)
  21. {
  22. child.Measure(availableSize);
  23. panelSize.Height += child.DesiredSize.Height;
  24. }
  25. return panelSize;
  26. }
  27. protected override Size ArrangeOverride(Size finalSize)
  28. {
  29. foreach (UIElement child in InternalChildren)
  30. {
  31. int exact = Shelf.GetExact(child); // Here I always get 100 which is default value for ExactProperty
  32. child.Arrange(new Rect(new Point(0, exact * 1), child.DesiredSize));
  33. }
  34. return finalSize;
  35. }
  36. }
  37.  
  38. <ItemsControl ItemsSource="{Binding Path=Packs}">
  39. <ItemsControl.ItemsPanel>
  40. <ItemsPanelTemplate>
  41. <local:Shelf />
  42. </ItemsPanelTemplate>
  43. </ItemsControl.ItemsPanel>
  44. <ItemsControl.ItemTemplate>
  45. <DataTemplate>
  46. <TextBlock local:Shelf.Exact="{Binding Path=Number}" Text="{Binding Path=Name}"/>
  47. </DataTemplate>
  48. </ItemsControl.ItemTemplate>
  49. </ItemsControl>
  50.  
  51. <local:Shelf Grid.Column="1">
  52. <TextBlock local:Shelf.Exact="223">Test</TextBlock>
  53. <TextBlock local:Shelf.Exact="332">Test</TextBlock>
  54. <TextBlock local:Shelf.Exact="443">Test</TextBlock>
  55. </local:Shelf>
  56.  
  57. <ItemsControl ItemsSource="{Binding Packs}">
  58. <ItemsControl.ItemsPanel>
  59. <ItemsPanelTemplate>
  60. <local:Shelf/>
  61. </ItemsPanelTemplate>
  62. </ItemsControl.ItemsPanel>
  63. <ItemsControl.ItemContainerStyle>
  64. <Style TargetType="ContentPresenter">
  65. <Setter Property="local:Shelf.Exact" Value="{Binding Number}"/>
  66. </Style>
  67. </ItemsControl.ItemContainerStyle>
  68. <ItemsControl.ItemTemplate>
  69. <DataTemplate>
  70. <TextBlock Text="{Binding Name}"/>
  71. </DataTemplate>
  72. </ItemsControl.ItemTemplate>
  73. </ItemsControl>
Add Comment
Please, Sign In to add comment