经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Git » 查看文章
WPF模拟实现Gitee泡泡菜单的示例代码
来源:jb51  时间:2022/8/1 19:09:42  对本文有异议

WPF实现 Gitee泡泡菜单

框架使用大于等于.NET40

Visual Studio 2022;

项目使用 MIT 开源许可协议;

  • 需要实现泡泡菜单需要使用Canvas画布进行添加内容;
  • 保证颜色随机,位置不重叠;
  • 点击泡泡获得当前泡泡的值;

实现代码

1) BubblleCanvas.cs 代码如下;

  1. using?System.Windows;
  2. using?System.Windows.Controls;
  3. using?WPFDevelopers.Helpers;
  4. using?WPFDevelopers.Utilities;
  5.  
  6. namespace?WPFDevelopers.Controls
  7. {
  8. ????public?class?BubblleCanvas?:?Canvas
  9. ????{
  10. ????????private?double?_bubbleItemX;
  11. ????????private?double?_bubbleItemY;
  12.  
  13. ????????private?int?_number;
  14. ????????private?double?_size;
  15. ????????private?const?int?_maxSize?=?120;
  16.  
  17. ????????protected?override?Size?ArrangeOverride(Size?arrangeSize)
  18. ????????{
  19. ????????????var?width?=?arrangeSize.Width;
  20. ????????????var?height?=?arrangeSize.Height;
  21.  
  22. ????????????double?left?=?0d,?top?=?0d;
  23. ????????????for?(var?y?=?0;?y?<?(int)height?/?_maxSize;?y++)
  24. ????????????{
  25. ????????????????double?yNum?=?y?+?1;
  26. ????????????????yNum?=?_maxSize?*?yNum;
  27. ????????????????for?(var?x?=?0;?x?<?(int)width?/?_maxSize;?x++)
  28. ????????????????{
  29. ????????????????????if?(_number?>?InternalChildren.Count?-?1)
  30. ????????????????????????return?arrangeSize;
  31.  
  32. ????????????????????var?item?=?InternalChildren[_number]?as?FrameworkElement;
  33.  
  34. ????????????????????if?(DoubleUtil.IsNaN(item.ActualWidth)?||?DoubleUtil.IsZero(item.ActualWidth)?||?DoubleUtil.IsNaN(item.ActualHeight)?||?DoubleUtil.IsZero(item.ActualHeight))
  35. ????????????????????????ResizeItem(item);
  36.  
  37. ????????????????????_bubbleItemX?=?Canvas.GetLeft(item);
  38. ????????????????????_bubbleItemY?=?Canvas.GetTop(item);
  39.  
  40. ????????????????????if?(double.IsNaN(_bubbleItemX)?||?double.IsNaN(_bubbleItemY))
  41. ????????????????????{
  42. ????????????????????????double?xNum?=?x?+?1;
  43. ????????????????????????xNum?=?_maxSize?*?xNum;
  44. ????????????????????????_bubbleItemX?=?ControlsHelper.NextDouble(left,?xNum?-?_size?*?ControlsHelper.NextDouble(0.6,?0.9));
  45. ????????????????????????var?_width?=?_bubbleItemX?+?_size;
  46. ????????????????????????_width?=?_width?>?width???width?-?(width?-?_bubbleItemX)?-?_size?:?_bubbleItemX;
  47. ????????????????????????_bubbleItemX?=?_width;
  48. ????????????????????????_bubbleItemY?=?ControlsHelper.NextDouble(top,?yNum?-?_size?*?ControlsHelper.NextDouble(0.6,?0.9));
  49. ????????????????????????var?_height?=?_bubbleItemY?+?_size;
  50. ????????????????????????_height?=?_height?>?height???height?-?(height?-?_bubbleItemY)?-?_size?:?_bubbleItemY;
  51. ????????????????????????_bubbleItemY?=?_height;
  52.  
  53. ????????????????????}
  54. ????????????????????Canvas.SetLeft(item,?_bubbleItemX);
  55. ????????????????????Canvas.SetTop(item,?_bubbleItemY);
  56. ????????????????????left?=?left?+?_size;
  57.  
  58. ????????????????????_number++;
  59.  
  60. ????????????????????item.Arrange(new?Rect(new?Point(_bubbleItemX,?_bubbleItemY),?new?Size(_size,?_size)));
  61. ????????????????}
  62. ????????????????left?=?0d;
  63. ????????????????top?=?top?+?_maxSize;
  64. ????????????}
  65.  
  66. ????????????return?arrangeSize;
  67. ????????}
  68. ????????private?void?ResizeItem(FrameworkElement?item)
  69. ????????{
  70. ????????????if?(DoubleUtil.GreaterThanOrClose(item.DesiredSize.Width,?55))
  71. ????????????????_size?=?ControlsHelper.GetRandom.Next(80,?_maxSize);
  72. ????????????else
  73. ????????????????_size?=?ControlsHelper.GetRandom.Next(55,?_maxSize);
  74. ????????????item.Width?=?_size;
  75. ????????????item.Height?=?_size;
  76. ????????}
  77. ????}
  78. }
  79.  

2) ControlsHelper.cs 代码如下;

  • 随机Double值;
  • 随机颜色;
  1. ?private?static?long?_tick?=?DateTime.Now.Ticks;
  2. ????????public?static?Random?GetRandom?=?new?Random((int)(_tick?&?0xffffffffL)?|?(int)(_tick?>>?32));
  3.  
  4. ????????public?static?double?NextDouble(double?miniDouble,?double?maxiDouble)
  5. ????????{
  6. ????????????if?(GetRandom?!=?null)
  7. ????????????{
  8. ????????????????return?GetRandom.NextDouble()?*?(maxiDouble?-?miniDouble)?+?miniDouble;
  9. ????????????}
  10. ????????????else
  11. ????????????{
  12. ????????????????return?0.0d;
  13. ????????????}
  14. ????????}
  15. ????????public?static?Brush?RandomBrush()
  16. ????????{
  17. ????????????var?R?=?GetRandom.Next(255);
  18. ????????????var?G?=?GetRandom.Next(255);
  19. ????????????var?B?=?GetRandom.Next(255);
  20. ????????????var?color?=?Color.FromRgb((byte)R,?(byte)G,?(byte)B);
  21. ????????????var?solidColorBrush?=?new?SolidColorBrush(color);
  22. ????????????return?solidColorBrush;
  23. ????????}

3) BubbleControl.cs 代码如下;

  1. using?System;
  2. using?System.Collections.Generic;
  3. using?System.Collections.ObjectModel;
  4. using?System.Diagnostics;
  5. using?System.Linq;
  6. using?System.Windows;
  7. using?System.Windows.Controls;
  8. using?System.Windows.Input;
  9. using?System.Windows.Media;
  10. using?System.Windows.Shapes;
  11. using?WPFDevelopers.Helpers;
  12.  
  13. namespace?WPFDevelopers.Controls
  14. {
  15. ????[TemplatePart(Name?=?BorderTemplateName,?Type?=?typeof(Border))]
  16. ????[TemplatePart(Name?=?EllipseTemplateName,?Type?=?typeof(Ellipse))]
  17. ????[TemplatePart(Name?=?RotateTransformTemplateName,?Type?=?typeof(RotateTransform))]
  18. ????public?class?BubblleControl?:?Control
  19. ????{
  20. ????????private?const?string?BorderTemplateName?=?"PART_Border";
  21. ????????private?const?string?EllipseTemplateName?=?"PART_Ellipse";
  22. ????????private?const?string?RotateTransformTemplateName?=?"PART_EllipseRotateTransform";
  23. ????????private?const?string?ListBoxTemplateName?=?"PART_ListBox";
  24.  
  25. ????????private?static?readonly?Type?_typeofSelf?=?typeof(BubblleControl);
  26.  
  27. ????????private?ObservableCollection<BubblleItem>?_items?=?new?ObservableCollection<BubblleItem>();
  28.  
  29.  
  30. ????????private?Border?_border;
  31. ????????private?Ellipse?_ellipse;
  32. ????????private?RotateTransform?_rotateTransform;
  33. ????????private?Brush[]?brushs;
  34. ????????private?ItemsControl?_listBox;
  35. ????????private?static?RoutedCommand?_clieckCommand;
  36.  
  37. ????????class?BubblleItem
  38. ????????{
  39. ????????????public?string?Text?{?get;?set;?}
  40. ????????????public?Brush?Bg?{?get;?set;?}
  41. ????????}
  42.  
  43. ????????static?BubblleControl()
  44. ????????{
  45. ????????????InitializeCommands();
  46. ????????????DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf,?new?FrameworkPropertyMetadata(_typeofSelf));
  47. ????????}
  48.  
  49. ????????#region?Event
  50.  
  51. ????????public?static?readonly?RoutedEvent?ClickEvent?=?EventManager.RegisterRoutedEvent("Click",?RoutingStrategy.Bubble,?typeof(RoutedEventHandler),?_typeofSelf);
  52. ????????public?event?RoutedEventHandler?Click
  53. ????????{
  54. ????????????add?{?AddHandler(ClickEvent,?value);?}
  55. ????????????remove?{?RemoveHandler(ClickEvent,?value);?}
  56. ????????}
  57.  
  58. ????????#endregion
  59.  
  60. ????????#region?Command
  61.  
  62. ????????private?static?RoutedCommand?_clickCommand?=?null;
  63.  
  64. ????????private?static?void?InitializeCommands()
  65. ????????{
  66. ????????????_clickCommand?=?new?RoutedCommand("Click",?_typeofSelf);
  67.  
  68. ????????????CommandManager.RegisterClassCommandBinding(_typeofSelf,?new?CommandBinding(_clickCommand,?OnClickCommand,?OnCanClickCommand));
  69. ????????}
  70.  
  71. ????????public?static?RoutedCommand?ClickCommand
  72. ????????{
  73. ????????????get?{?return?_clickCommand;?}
  74. ????????}
  75.  
  76. ????????private?static?void?OnClickCommand(object?sender,?ExecutedRoutedEventArgs?e)
  77. ????????{
  78. ????????????var?ctrl?=?sender?as?BubblleControl;
  79.  
  80. ????????????ctrl.SetValue(SelectedTextPropertyKey,?e.Parameter?.ToString());
  81. ????????????ctrl.RaiseEvent(new?RoutedEventArgs(ClickEvent));
  82. ????????}
  83.  
  84. ????????private?static?void?OnCanClickCommand(object?sender,?CanExecuteRoutedEventArgs?e)
  85. ????????{
  86. ????????????e.CanExecute?=?true;
  87. ????????}
  88.  
  89. ????????#endregion
  90.  
  91. ????????#region?readonly?Properties
  92.  
  93. ????????private?static?readonly?DependencyPropertyKey?SelectedTextPropertyKey?=
  94. ???????????DependencyProperty.RegisterReadOnly("SelectedText",?typeof(string),?_typeofSelf,?new?PropertyMetadata(null));
  95. ????????public?static?readonly?DependencyProperty?SelectedTextProperty?=?SelectedTextPropertyKey.DependencyProperty;
  96. ????????public?string?SelectedText
  97. ????????{
  98. ????????????get?{?return?(string)GetValue(SelectedTextProperty);?}
  99. ????????}
  100. ????????public?new?static?readonly?DependencyProperty?BorderBackgroundProperty?=
  101. ????????????DependencyProperty.Register("BorderBackground",?typeof(Brush),?typeof(BubblleControl),
  102. ????????????????new?PropertyMetadata(null));
  103.  
  104. ????????public?new?static?readonly?DependencyProperty?EarthBackgroundProperty?=
  105. ????????????DependencyProperty.Register("EarthBackground",?typeof(Brush),?typeof(BubblleControl),
  106. ????????????????new?PropertyMetadata(Brushes.DarkOrchid));
  107. ????????public?Brush?BorderBackground
  108. ????????{
  109. ????????????get?=>?(Brush)this.GetValue(BorderBackgroundProperty);
  110. ????????????set?=>?this.SetValue(BorderBackgroundProperty,?(object)value);
  111. ????????}
  112. ????????public?Brush?EarthBackground
  113. ????????{
  114. ????????????get?=>?(Brush)this.GetValue(EarthBackgroundProperty);
  115. ????????????set?=>?this.SetValue(EarthBackgroundProperty,?(object)value);
  116. ????????}
  117. ????????#endregion
  118.  
  119. ????????#region?Property
  120.  
  121. ????????public?static?readonly?DependencyProperty?ItemsSourceProperty?=
  122. ????????????DependencyProperty.Register("ItemsSource",?typeof(IEnumerable<string>),?typeof(BubblleControl),?new?PropertyMetadata(null,?OnItemsSourcePropertyChanged));
  123. ????????public?IEnumerable<string>?ItemsSource
  124. ????????{
  125. ????????????get?{?return?(IEnumerable<string>)GetValue(ItemsSourceProperty);?}
  126. ????????????set?{?SetValue(ItemsSourceProperty,?value);?}
  127. ????????}
  128.  
  129. ????????private?static?void?OnItemsSourcePropertyChanged(DependencyObject?obj,?DependencyPropertyChangedEventArgs?e)
  130. ????????{
  131. ????????????var?ctrl?=?obj?as?BubblleControl;
  132. ????????????var?newValue?=?e.NewValue?as?IEnumerable<string>;
  133.  
  134. ????????????if?(newValue?==?null)
  135. ????????????{
  136. ????????????????ctrl._items.Clear();
  137. ????????????????return;
  138. ????????????}
  139.  
  140. ????????????foreach?(var?item?in?newValue)
  141. ????????????{
  142. ????????????????ctrl._items.Add(new?BubblleItem?{?Text?=?item,?Bg?=?ControlsHelper.RandomBrush()?});
  143. ????????????}
  144. ????????}
  145.  
  146. ????????#endregion
  147.  
  148. ????????#region?Override
  149.  
  150. ????????public?override?void?OnApplyTemplate()
  151. ????????{
  152. ????????????base.OnApplyTemplate();
  153. ????????????_border?=?GetTemplateChild(BorderTemplateName)?as?Border;
  154. ????????????_ellipse?=?GetTemplateChild(EllipseTemplateName)?as?Ellipse;
  155. ????????????_rotateTransform?=?GetTemplateChild(RotateTransformTemplateName)?as?RotateTransform;
  156. ????????????Loaded?+=?delegate
  157. ????????????{
  158. ????????????????var?point?=?_border.TranslatePoint(new?Point(_border.ActualWidth?/?2,?_border.ActualHeight?/?2),
  159. ????????????????????_ellipse);
  160. ????????????????_rotateTransform.CenterX?=?point.X?-?_ellipse.ActualWidth?/?2;
  161. ????????????????_rotateTransform.CenterY?=?point.Y?-?_ellipse.ActualHeight?/?2;
  162. ????????????};
  163. ????????????_listBox?=?GetTemplateChild(ListBoxTemplateName)?as?ItemsControl;
  164. ????????????_listBox.ItemsSource?=?_items;
  165. ????????}
  166.  
  167. ????????#endregion
  168. ????}
  169. }

4) BubblleControl.xaml 代码如下;

  1. <ResourceDictionary?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. ????????????????????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. ????????????????????xmlns:controls="clr-namespace:WPFDevelopers.Controls">
  4.  
  5. ????<ResourceDictionary.MergedDictionaries>
  6. ????????<ResourceDictionary?Source="Basic/ControlBasic.xaml"/>
  7. ????????<ResourceDictionary?Source="Basic/Animations.xaml"/>
  8. ????</ResourceDictionary.MergedDictionaries>
  9.  
  10. ????<Style?TargetType="controls:BubblleControl"?BasedOn="{StaticResource?ControlBasicStyle}">
  11. ????????<Setter?Property="Width"?Value="400"/>
  12. ????????<Setter?Property="Height"?Value="400"/>
  13. ????????<Setter?Property="Background"?Value="{StaticResource?WhiteSolidColorBrush}"/>
  14. ????????<Setter?Property="BorderThickness"?Value="1"/>
  15. ????????<Setter?Property="BorderBrush"?Value="{StaticResource?SecondaryTextSolidColorBrush}"/>
  16. ????????<Setter?Property="BorderBackground"?Value="{StaticResource?BaseSolidColorBrush}"/>
  17. ????????<Setter?Property="Template">
  18. ????????????<Setter.Value>
  19. ????????????????<ControlTemplate?TargetType="controls:BubblleControl">
  20. ????????????????????<Grid?Width="{TemplateBinding?Width}"?Height="{TemplateBinding?Height}">
  21. ????????????????????????<Border?BorderBrush="{TemplateBinding?BorderBrush}"
  22. ????????????????????????????????????????????????BorderThickness="{TemplateBinding?BorderThickness}"?
  23. ????????????????????????????????????????????????Background="{TemplateBinding?BorderBackground}"?
  24. ????????????????????????????????????????????????Margin="45"
  25. ????????????????????????????????????????????????CornerRadius="400"
  26. ????????????????????????????????????????????????x:Name="PART_Border">
  27. ????????????????????????????<Ellipse?Fill="{TemplateBinding?Background}"?Margin="20"/>
  28. ????????????????????????</Border>
  29. ????????????????????????<Ellipse?Fill="{TemplateBinding?EarthBackground}"
  30. ?????????????????????????????????????????????????Width="26"?Height="26"
  31. ?????????????????????????????????????????????????RenderTransformOrigin=".5,.5"
  32. ?????????????????????????????????????????????????x:Name="PART_Ellipse"
  33. ?????????????????????????????????????????????????VerticalAlignment="Top"?Margin="0,35,0,0">
  34. ????????????????????????????<Ellipse.RenderTransform>
  35. ????????????????????????????????<RotateTransform?x:Name="PART_EllipseRotateTransform"></RotateTransform>
  36. ????????????????????????????</Ellipse.RenderTransform>
  37. ????????????????????????????<Ellipse.Triggers>
  38. ????????????????????????????????<EventTrigger?RoutedEvent="Loaded">
  39. ????????????????????????????????????<BeginStoryboard>
  40. ????????????????????????????????????????<Storyboard>
  41. ????????????????????????????????????????????<DoubleAnimation?Storyboard.TargetProperty="(Ellipse.RenderTransform).(RotateTransform.Angle)"
  42. ?????????????????????????????????????????????????????????????????????????????RepeatBehavior="Forever"
  43. ?????????????????????????????????????????????????????????????????????????????From="0"?To="360"
  44. ?????????????????????????????????????????????????????????????????????????????Duration="00:00:13"></DoubleAnimation>
  45. ????????????????????????????????????????</Storyboard>
  46. ????????????????????????????????????</BeginStoryboard>
  47. ????????????????????????????????</EventTrigger>
  48. ????????????????????????????</Ellipse.Triggers>
  49. ????????????????????????</Ellipse>
  50. ????????????????????????<ItemsControl?x:Name="PART_ListBox"
  51. ??????????????????????????????????????ItemsSource="{TemplateBinding?ItemsSource}">
  52. ????????????????????????????<ItemsControl.ItemTemplate>
  53. ????????????????????????????????<DataTemplate>
  54. ????????????????????????????????????<Grid>
  55. ????????????????????????????????????????<Grid?Width="{TemplateBinding?Width}"?
  56. ??????????????????????????????????????????????Height="{TemplateBinding?Height}">
  57.  
  58. ????????????????????????????????????????????<Ellipse?Fill="{Binding?Bg}"
  59. ?????????????????????????????????????????????????????????????????Opacity=".4"/>
  60. ????????????????????????????????????????????<Ellipse?Stroke="{Binding?Bg}"?
  61. ?????????????????????????????????????????????????????????????????StrokeThickness=".8"/>
  62. ????????????????????????????????????????</Grid>
  63.  
  64. ????????????????????????????????????????<TextBlock?VerticalAlignment="Center"?
  65. ???????????????????????????????????????????????????????????????HorizontalAlignment="Center"
  66. ???????????????????????????????????????????????????????????????Padding="10,0">
  67. ????????????????????????????????????????????????????????<Hyperlink?
  68. ????????????????????????????????????????????????????????????Foreground="{Binding?Bg}"
  69. ????????????????????????????????????????????????????????????Command="{x:Static?controls:BubblleControl.ClickCommand}"
  70. ????????????????????????????????????????????????????????????CommandParameter="{Binding?Text}"
  71. ????????????????????????????????????????????????????????????FontWeight="Normal">
  72. ????????????????????????????????????????????????????????????<TextBlock?Text="{Binding?Text}"
  73. ???????????????????????????????????????????????????????????????????????TextAlignment="Center"
  74. ???????????????????????????????????????????????????????????????????????TextTrimming="CharacterEllipsis"
  75. ???????????????????????????????????????????????????????????????????????ToolTip="{Binding?Text}"/>
  76. ????????????????????????????????????????????????????????</Hyperlink>
  77. ????????????????????????????????????????????????????</TextBlock>
  78. ????????????????????????????????????</Grid>
  79. ????????????????????????????????</DataTemplate>
  80. ????????????????????????????</ItemsControl.ItemTemplate>
  81. ????????????????????????????<ItemsControl.ItemsPanel>
  82. ????????????????????????????????<ItemsPanelTemplate>
  83. ????????????????????????????????????<controls:BubblleCanvas/>
  84. ????????????????????????????????</ItemsPanelTemplate>
  85. ????????????????????????????</ItemsControl.ItemsPanel>
  86. ????????????????????????</ItemsControl>
  87. ????????????????????</Grid>
  88. ????????????????</ControlTemplate>
  89. ????????????</Setter.Value>
  90. ????????</Setter>
  91. ????</Style>
  92.  
  93. </ResourceDictionary>

5) BubblleControlExample.xaml 代码如下;

  • TabItem随机 是自动设置位置和颜色;
  • TabItem自定义 可以自行定义展示的内容;
  1. <UserControl?x:Class="WPFDevelopers.Samples.ExampleViews.BubblleControlExample"
  2. ?????????????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. ?????????????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. ?????????????xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"?
  5. ?????????????xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?
  6. ?????????????xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"
  7. ?????????????xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
  8. ?????????????xmlns:sys="clr-namespace:System;assembly=mscorlib"
  9. ?????????????mc:Ignorable="d"?
  10. ?????????????d:DesignHeight="450"?d:DesignWidth="800">
  11. ????
  12. ????<Grid>
  13. ????????<TabControl>
  14. ????????????<TabItem?Header="随机">
  15. ????????????????<wpfdev:BubblleControl?x:Name="MyBubblleControl"??Click="BubblleControl_Click">
  16. ????????????????????<wpfdev:BubblleControl.ItemsSource>
  17. ????????????????????????<x:Array?Type="sys:String">
  18. ????????????????????????????<sys:String>WPF</sys:String>
  19. ????????????????????????????<sys:String>ASP.NET</sys:String>
  20. ????????????????????????????<sys:String>WinUI</sys:String>
  21. ????????????????????????????<sys:String>WebAPI</sys:String>
  22. ????????????????????????????<sys:String>Blazor</sys:String>
  23. ????????????????????????????<sys:String>MAUI</sys:String>
  24. ????????????????????????????<sys:String>Xamarin</sys:String>
  25. ????????????????????????????<sys:String>WinForm</sys:String>
  26. ????????????????????????????<sys:String>UWP</sys:String>
  27. ????????????????????????</x:Array>
  28. ????????????????????</wpfdev:BubblleControl.ItemsSource>
  29. ????????????????</wpfdev:BubblleControl>
  30. ????????????</TabItem>
  31. ????????????<TabItem?Header="自定义">
  32. ????????????????<wpfdev:BubblleCanvas?Width="400"?Height="400">
  33. ????????????????????<Grid>
  34. ????????????????????????<Grid?Width="60"?
  35. ??????????????????????????????Height="60">
  36. ????????????????????????????<Ellipse?Fill="MediumSpringGreen"
  37. ?????????????????????????????????????Opacity=".4"/>
  38. ????????????????????????????<Ellipse?Stroke="MediumSpringGreen"?
  39. ?????????????????????????????????????StrokeThickness=".8"/>
  40. ????????????????????????</Grid>
  41. ????????????????????????<TextBlock?VerticalAlignment="Center"?
  42. ???????????????????????????????????HorizontalAlignment="Center"
  43. ???????????????????????????????????Padding="10,0">
  44. ????????????????????????????<Hyperlink?
  45. ????????????????????????????????Foreground="MediumSpringGreen"
  46. ????????????????????????????????FontWeight="Normal"
  47. ????????????????????????????????Command="{Binding?ClickCommand,RelativeSource={RelativeSource?AncestorType=local:BubblleControlExample}}">
  48. ????????????????????????????????<TextBlock?Text="WPF"
  49. ???????????????????????????????????????????TextAlignment="Center"
  50. ???????????????????????????????????????????TextTrimming="CharacterEllipsis"/>
  51. ????????????????????????????</Hyperlink>
  52. ????????????????????????</TextBlock>
  53. ????????????????????</Grid>
  54. ????????????????????<Grid>
  55. ????????????????????????<Grid?Width="60"?
  56. ??????????????????????????????Height="60">
  57. ????????????????????????????<Ellipse?Fill="Brown"
  58. ?????????????????????????????????????Opacity=".4"/>
  59. ????????????????????????????<Ellipse?Stroke="Brown"?
  60. ?????????????????????????????????????StrokeThickness=".8"/>
  61. ????????????????????????</Grid>
  62. ????????????????????????<TextBlock?VerticalAlignment="Center"?
  63. ???????????????????????????????????HorizontalAlignment="Center"
  64. ???????????????????????????????????Padding="10,0">
  65. ????????????????????????????<Hyperlink?
  66. ????????????????????????????????Foreground="Brown"
  67. ????????????????????????????????FontWeight="Normal"
  68. ????????????????????????????????Command="{Binding?ClickCommand,RelativeSource={RelativeSource?AncestorType=local:BubblleControlExample}}">
  69. ????????????????????????????????<TextBlock?Text="MAUI"
  70. ???????????????????????????????????????????TextAlignment="Center"
  71. ???????????????????????????????????????????TextTrimming="CharacterEllipsis"/>
  72. ????????????????????????????</Hyperlink>
  73. ????????????????????????</TextBlock>
  74. ????????????????????</Grid>
  75. ????????????????????<Grid>
  76. ????????????????????????<Grid?Width="60"?
  77. ??????????????????????????????Height="60">
  78. ????????????????????????????<Ellipse?Fill="DeepSkyBlue"
  79. ?????????????????????????????????????Opacity=".4"/>
  80. ????????????????????????????<Ellipse?Stroke="DeepSkyBlue"?
  81. ?????????????????????????????????????StrokeThickness=".8"/>
  82. ????????????????????????</Grid>
  83. ????????????????????????<TextBlock?VerticalAlignment="Center"?
  84. ???????????????????????????????????HorizontalAlignment="Center"
  85. ???????????????????????????????????Padding="10,0">
  86. ????????????????????????????<Hyperlink?
  87. ????????????????????????????????Foreground="DeepSkyBlue"
  88. ????????????????????????????????FontWeight="Normal"
  89. ????????????????????????????????Command="{Binding?ClickCommand,RelativeSource={RelativeSource?AncestorType=local:BubblleControlExample}}">
  90. ????????????????????????????????<TextBlock?Text="Blazor"
  91. ???????????????????????????????????????????TextAlignment="Center"
  92. ???????????????????????????????????????????TextTrimming="CharacterEllipsis"/>
  93. ????????????????????????????</Hyperlink>
  94. ????????????????????????</TextBlock>
  95. ????????????????????</Grid>
  96. ????????????????</wpfdev:BubblleCanvas>
  97. ????????????</TabItem>
  98. ????????</TabControl>
  99. ????????
  100. ????</Grid>
  101. </UserControl>

6) BubblleControlExample.xaml.cs 代码如下;

  1. using?System.Windows;
  2. using?System.Windows.Controls;
  3. using?System.Windows.Input;
  4. using?WPFDevelopers.Samples.Helpers;
  5.  
  6. namespace?WPFDevelopers.Samples.ExampleViews
  7. {
  8. ????///?<summary>
  9. ????///?BubbleControlExample.xaml?的交互逻辑
  10. ????///?</summary>
  11. ????public?partial?class?BubblleControlExample?:?UserControl
  12. ????{
  13. ????????public?BubblleControlExample()
  14. ????????{
  15. ????????????InitializeComponent();
  16. ????????}
  17. ????????public?ICommand?ClickCommand?=>?new?RelayCommand(delegate
  18. ????????{
  19. ???????????WPFDevelopers.Minimal.Controls.MessageBox.Show("点击完成。");
  20. ????????});
  21.  
  22. ????????private?void?BubblleControl_Click(object?sender,?System.Windows.RoutedEventArgs?e)
  23. ????????{
  24. ????????????MessageBox.Show($"点击了“?{MyBubblleControl.SelectedText}开发者?”.",?"提示",MessageBoxButton.OK,MessageBoxImage.Information);
  25. ????????}
  26. ????}
  27. }

以上就是WPF模拟实现Gitee泡泡菜单的示例代码的详细内容,更多关于WPF泡泡菜单的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号