Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. internal class ButtonAnalysisControl : Control
  2. {
  3. static ButtonAnalysisControl()
  4. {
  5. DefaultStyleKeyProperty.OverrideMetadata(typeof(ButtonAnalysisControl), new FrameworkPropertyMetadata(typeof(ButtonAnalysisControl)));
  6. }
  7.  
  8. public string Text
  9. {
  10. get { return (string)GetValue(TextProperty); }
  11. set { SetValue(TextProperty, value); }
  12. }
  13.  
  14. public Brush BackgroundBrush
  15. {
  16. get { return (Brush)GetValue(BackgroundBrushProperty); }
  17. set { SetValue(BackgroundBrushProperty, value); }
  18. }
  19.  
  20. public ObservableCollection<ViewCommand> ChildCommands
  21. {
  22. get { return (ObservableCollection<ViewCommand>)GetValue(ChildCommandsProperty); }
  23. set { SetValue(ChildCommandsProperty, value); }
  24. }
  25.  
  26. public static readonly DependencyProperty TextProperty =
  27. DependencyProperty.Register("Text", typeof(string), typeof(ButtonAnalysisControl), new UIPropertyMetadata(string.Empty));
  28.  
  29. public static readonly DependencyProperty BackgroundBrushProperty =
  30. DependencyProperty.Register("BackgroundBrush", typeof(Brush), typeof(ButtonAnalysisControl), new UIPropertyMetadata(Brushes.Transparent));
  31.  
  32. public static readonly DependencyProperty ChildCommandsProperty =
  33. DependencyProperty.Register("ChildCommands", typeof(ObservableCollection<ViewCommand>), typeof(ButtonAnalysisControl), new UIPropertyMetadata(null));
  34.  
  35. }
  36.  
  37. <Style TargetType="anal:ButtonAnalysisControl">
  38.  
  39. <Style.Triggers>
  40. <EventTrigger RoutedEvent="MouseDown">
  41. <EventTrigger.Actions>
  42. <BeginStoryboard>
  43. <Storyboard>
  44. <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
  45. <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
  46. </BooleanAnimationUsingKeyFrames>
  47. </Storyboard>
  48. </BeginStoryboard>
  49. </EventTrigger.Actions>
  50. </EventTrigger>
  51. </Style.Triggers>
  52.  
  53. <Setter Property="Template">
  54. <Setter.Value>
  55. <ControlTemplate TargetType="anal:ButtonAnalysisControl">
  56. <Grid>
  57. <Grid.RowDefinitions>
  58. <RowDefinition Height="Auto" />
  59. <RowDefinition Height="*"/>
  60. </Grid.RowDefinitions>
  61.  
  62. <Grid.ColumnDefinitions>
  63. <ColumnDefinition Width="*" />
  64. </Grid.ColumnDefinitions>
  65.  
  66. <TextBlock TextAlignment="Center"
  67. VerticalAlignment="Stretch"
  68. Foreground="{StaticResource CommandBarForeground}"
  69. Background="{StaticResource MainForegroundBrush}"
  70. FontFamily="{StaticResource FontFamily}"
  71. FontSize="10"
  72. Grid.Column="0"
  73. Grid.Row="0">
  74. <TextBlock.Text>
  75. <Binding Path="Text" StringFormat="{}{0}%" RelativeSource="{RelativeSource TemplatedParent}" />
  76. </TextBlock.Text>
  77. </TextBlock>
  78. <Rectangle Grid.Column="0"
  79. Grid.Row="1">
  80. <Rectangle.Fill>
  81. <Binding Path="BackgroundBrush" RelativeSource="{RelativeSource TemplatedParent}" />
  82. </Rectangle.Fill>
  83. </Rectangle>
  84. </Grid>
  85. </ControlTemplate>
  86. </Setter.Value>
  87. </Setter>
  88.  
  89. <Setter Property="ContextMenu">
  90. <Setter.Value>
  91. <ContextMenu>
  92. <ContextMenu.ItemsSource>
  93. <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ChildCommands"/>
  94. </ContextMenu.ItemsSource>
  95. <ContextMenu.ItemContainerStyle>
  96. <Style TargetType="{x:Type MenuItem}">
  97. <Setter Property="MenuItem.Header" Value="{Binding Command.Text}"/>
  98. </Style>
  99. </ContextMenu.ItemContainerStyle>
  100. </ContextMenu>
  101. </Setter.Value>
  102. </Setter>
  103. </Style>
  104.  
  105. public ButtonAnalysisAdorner(UIElement adornedElement, int numberOfTimesFieldFilled, int numberOfLoggedViews, ObservableCollection<ViewCommand> childCommands)
  106. : base(adornedElement)
  107. {
  108. _visuals = new VisualCollection(this);
  109. _contentPresenter = new ContentPresenter();
  110. ButtonAnalysisControl bac = new ButtonAnalysisControl();
  111. bac.Text = percentage.ToString(CultureInfo.InvariantCulture);
  112. bac.BackgroundBrush = PercentColorRanges.GetColorFromPercentage((int)percentage, 0.75);
  113. bac.ToolTip = ToolTipValue(numberOfTimesFieldFilled, numberOfLoggedViews);
  114. bac.ChildCommands = childCommands;
  115. Content = bac;
  116.  
  117. _visuals.Add(_contentPresenter);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement