WPF模拟实现Gitee泡泡菜单的示例代码_第1页
WPF模拟实现Gitee泡泡菜单的示例代码_第2页
WPF模拟实现Gitee泡泡菜单的示例代码_第3页
WPF模拟实现Gitee泡泡菜单的示例代码_第4页
WPF模拟实现Gitee泡泡菜单的示例代码_第5页
已阅读5页,还剩11页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第WPF模拟实现Gitee泡泡菜单的示例代码private

ItemsControl

_listBox;

private

static

RoutedCommand

_clieckCommand;

class

BubblleItem

{

public

string

Text

{

get;

set;

}

public

Brush

Bg

{

get;

set;

}

}

static

BubblleControl()

{

InitializeCommands();

DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf,

new

FrameworkPropertyMetadata(_typeofSelf));

}

#region

Event

public

static

readonly

RoutedEvent

ClickEvent

=

EventManager.RegisterRoutedEvent("Click",

RoutingStrategy.Bubble,

typeof(RoutedEventHandler),

_typeofSelf);

public

event

RoutedEventHandler

Click

{

add

{

AddHandler(ClickEvent,

value);

}

remove

{

RemoveHandler(ClickEvent,

value);

}

}

#endregion

#region

Command

private

static

RoutedCommand

_clickCommand

=

null;

private

static

void

InitializeCommands()

{

_clickCommand

=

new

RoutedCommand("Click",

_typeofSelf);

CommandManager.RegisterClassCommandBinding(_typeofSelf,

new

CommandBinding(_clickCommand,

OnClickCommand,

OnCanClickCommand));

}

public

static

RoutedCommand

ClickCommand

{

get

{

return

_clickCommand;

}

}

private

static

void

OnClickCommand(object

sender,

ExecutedRoutedEventArgs

e)

{

var

ctrl

=

sender

as

BubblleControl;

ctrl.SetValue(SelectedTextPropertyKey,

e.Parameter.ToString());

ctrl.RaiseEvent(new

RoutedEventArgs(ClickEvent));

}

private

static

void

OnCanClickCommand(object

sender,

CanExecuteRoutedEventArgs

e)

{

e.CanExecute

=

true;

}

#endregion

#region

readonly

Properties

private

static

readonly

DependencyPropertyKey

SelectedTextPropertyKey

=

DependencyProperty.RegisterReadOnly("SelectedText",

typeof(string),

_typeofSelf,

new

PropertyMetadata(null));

public

static

readonly

DependencyProperty

SelectedTextProperty

=

SelectedTextPropertyKey.DependencyProperty;

public

string

SelectedText

{

get

{

return

(string)GetValue(SelectedTextProperty);

}

}

public

new

static

readonly

DependencyProperty

BorderBackgroundProperty

=

DependencyProperty.Register("BorderBackground",

typeof(Brush),

typeof(BubblleControl),

new

PropertyMetadata(null));

public

new

static

readonly

DependencyProperty

EarthBackgroundProperty

=

DependencyProperty.Register("EarthBackground",

typeof(Brush),

typeof(BubblleControl),

new

PropertyMetadata(Brushes.DarkOrchid));

public

Brush

BorderBackground

{

get

=

(Brush)this.GetValue(BorderBackgroundProperty);

set

=

this.SetValue(BorderBackgroundProperty,

(object)value);

}

public

Brush

EarthBackground

{

get

=

(Brush)this.GetValue(EarthBackgroundProperty);

set

=

this.SetValue(EarthBackgroundProperty,

(object)value);

}

#endregion

#region

Property

public

static

readonly

DependencyProperty

ItemsSourceProperty

=

DependencyProperty.Register("ItemsSource",

typeof(IEnumerablestring),

typeof(BubblleControl),

new

PropertyMetadata(null,

OnItemsSourcePropertyChanged));

public

IEnumerablestring

ItemsSource

{

get

{

return

(IEnumerablestring)GetValue(ItemsSourceProperty);

}

set

{

SetValue(ItemsSourceProperty,

value);

}

}

private

static

void

OnItemsSourcePropertyChanged(DependencyObject

obj,

DependencyPropertyChangedEventArgs

e)

{

var

ctrl

=

obj

as

BubblleControl;

var

newValue

=

e.NewValue

as

IEnumerablestring

if

(newValue

==

null)

{

ctrl._items.Clear();

return;

}

foreach

(var

item

in

newValue)

{

ctrl._items.Add(new

BubblleItem

{

Text

=

item,

Bg

=

ControlsHelper.RandomBrush()

});

}

}

#endregion

#region

Override

public

override

void

OnApplyTemplate()

{

base.OnApplyTemplate();

_border

=

GetTemplateChild(BorderTemplateName)

as

Border;

_ellipse

=

GetTemplateChild(EllipseTemplateName)

as

Ellipse;

_rotateTransform

=

GetTemplateChild(RotateTransformTemplateName)

as

RotateTransform;

Loaded

+=

delegate

{

var

point

=

_border.TranslatePoint(new

Point(_border.ActualWidth

/

2,

_border.ActualHeight

/

2),

_ellipse);

_rotateTransform.CenterX

=

point.X

-

_ellipse.ActualWidth

/

2;

_rotateTransform.CenterY

=

point.Y

-

_ellipse.ActualHeight

/

2;

};

_listBox

=

GetTemplateChild(ListBoxTemplateName)

as

ItemsControl;

_listBox.ItemsSource

=

_items;

}

#endregion

}

4)BubblleControl.xaml代码如下;

ResourceDictionary

xmlns="/winfx/2006/xaml/presentation"

xmlns:x="/winfx/2006/xaml"

xmlns:controls="clr-namespace:WPFDevelopers.Controls"

ResourceDictionary.MergedDictionaries

ResourceDictionary

Source="Basic/ControlBasic.xaml"/

ResourceDictionary

Source="Basic/Animations.xaml"/

/ResourceDictionary.MergedDictionaries

Style

TargetType="controls:BubblleControl"

BasedOn="{StaticResource

ControlBasicStyle}"

Setter

Property="Width"

Value="400"/

Setter

Property="Height"

Value="400"/

Setter

Property="Background"

Value="{StaticResource

WhiteSolidColorBrush}"/

Setter

Property="BorderThickness"

Value="1"/

Setter

Property="BorderBrush"

Value="{StaticResource

SecondaryTextSolidColorBrush}"/

Setter

Property="BorderBackground"

Value="{StaticResource

BaseSolidColorBrush}"/

Setter

Property="Template"

Setter.Value

ControlTemplate

TargetType="controls:BubblleControl"

Grid

Width="{TemplateBinding

Width}"

Height="{TemplateBinding

Height}"

Border

BorderBrush="{TemplateBinding

BorderBrush}"

BorderThickness="{TemplateBinding

BorderThickness}"

Background="{TemplateBinding

BorderBackground}"

Margin="45"

CornerRadius="400"

x:Name="PART_Border"

Ellipse

Fill="{TemplateBinding

Background}"

Margin="20"/

/Border

Ellipse

Fill="{TemplateBinding

EarthBackground}"

Width="26"

Height="26"

RenderTransformOrigin=".5,.5"

x:Name="PART_Ellipse"

VerticalAlignment="Top"

Margin="0,35,0,0"

Ellipse.RenderTransform

RotateTransform

x:Name="PART_EllipseRotateTransform"/RotateTransform

/Ellipse.RenderTransform

Ellipse.Triggers

EventTrigger

RoutedEvent="Loaded"

BeginStoryboard

Storyboard

DoubleAnimation

Storyboard.TargetProperty="(Ellipse.RenderTransform).(RotateTransform.Angle)"

RepeatBehavior="Forever"

From="0"

To="360"

Duration="00:00:13"/DoubleAnimation

/Storyboard

/BeginStoryboard

/EventTrigger

/Ellipse.Triggers

/Ellipse

ItemsControl

x:Name="PART_ListBox"

ItemsSource="{TemplateBinding

ItemsSource}"

ItemsControl.ItemTemplate

DataTemplate

Grid

Grid

Width="{TemplateBinding

Width}"

Height="{TemplateBinding

Height}"

Ellipse

Fill="{Binding

Bg}"

Opacity=".4"/

Ellipse

Stroke="{Binding

Bg}"

StrokeThickness=".8"/

/Grid

TextBlock

VerticalAlignment="Center"

HorizontalAlignment="Center"

Padding="10,0"

Hyperlink

Foreground="{Binding

Bg}"

Command="{x:Static

controls:BubblleControl.ClickCommand}"

CommandParameter="{Binding

Text}"

FontWeight="Normal"

TextBlock

Text="{Binding

Text}"

TextAlignment="Center"

TextTrimming="CharacterEllipsis"

ToolTip="{Binding

Text}"/

/Hyperlink

/TextBlock

/Grid

/DataTemplate

/ItemsControl.ItemTemplate

ItemsControl.ItemsPanel

ItemsPanelTemplate

controls:BubblleCanvas/

/ItemsPanelTemplate

/ItemsControl.ItemsPanel

/ItemsControl

/Grid

/ControlTemplate

/Setter.Value

/Setter

/Style

/ResourceDictionary

5)BubblleControlExample.xaml代码如下;

TabItem随机是自动设置位置和颜色;TabItem自定义可以自行定义展示的内容;

UserControl

x:

xmlns="/winfx/2006/xaml/presentation"

xmlns:x="/winfx/2006/xaml"

xmlns:mc="/markup-compatibility/2006"

xmlns:d="/expression/blend/2008"

xmlns:wpfdev="/WPFDevelopersOrg/WPFDevelopers"

xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"

xmlns:sys="clr-namespace:System;assembly=mscorlib"

mc:Ignorable="d"

d:DesignHeight="450"

d:DesignWidth="800"

Grid

TabControl

TabItem

Header="随机"

wpfdev:BubblleControl

x:Name="MyBubblleControl"

Click="BubblleControl_Click"

wpfdev:BubblleControl.ItemsSource

x:Array

Type="sys:String"

sys:StringWPF/sys:String

sys:StringASP.NET/sys:String

sys:StringWinUI/sys:String

sys:StringWebAPI/sys:String

sys:StringBlazor/sys:String

sys:StringMAUI/sys:String

sys:StringXamarin/sys:String

sys:StringWinForm/sys:String

sys:StringUWP/sys:String

/x:Array

/wpfdev:BubblleControl.ItemsSource

/wpfdev:BubblleControl

/TabItem

TabItem

Header="自定义"

wpfdev:BubblleCanvas

Width="400"

Height="400"

Grid

Grid

Width="60"

Height="60"

Ellipse

Fill="MediumSpringGreen"

Opacity=".4"/

Ellipse

Stroke="MediumSpringGreen"

StrokeThickness=".8"/

/Grid

TextBlock

VerticalAlignment="Center"

HorizontalAlignment="Center"

Padding="10,0"

Hyperlink

Foreground="MediumSpringGreen"

FontWeight="Normal"

Command="{Binding

ClickCommand,RelativeSource={RelativeSource

AncestorType=local:BubblleControlExample}}"

TextBlock

Text="WPF"

TextAlignment="Center"

TextTrimming="CharacterEllipsis"/

/Hyperlink

/TextBlock

/Grid

Grid

Grid

Width="60"

Height="60"

Ellipse

Fill="Brown"

Opacity=".4"/

Ellipse

Stroke="Brown"

StrokeThickness=".8"/

/Grid

TextBlock

VerticalAlignment="Center"

HorizontalAlignment="Center"

Padding="10,0"

Hyperlink

Foreground="Brown"

FontWeight="Normal"

Command="{Binding

ClickCommand,RelativeSource={RelativeSource

AncestorType=local:BubblleControlExample}}"

TextBlock

Text="MAUI"

TextAlignment="Center"

TextTrimming="CharacterEllipsis"/

/Hyperlink

/TextBlock

/Grid

Grid

Grid

Width="60"

Height="60"

Ellipse

Fill="DeepSkyBlue"

Opacity=".4"/

Ellipse

Stroke="DeepSkyBlue"

StrokeThickness=".8"/

/Grid

TextBlock

VerticalAlignment="Center"

Horizo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论