当前位置: 首页 > news >正文

C# Avalonia 18- ControlTemplates - FlipPanelTest

FlipPanel2类是负责控制逻辑。

FlipPanel2.cs

using Avalonia;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using System;namespace AvaloniaUI
{[TemplatePart("PART_FrontHost", typeof(Border))][TemplatePart("PART_BackHost", typeof(Border))][TemplatePart("PART_FlipButton", typeof(ToggleButton))]public class FlipPanel2 : TemplatedControl{// ========== 依赖属性 ==========public static readonly StyledProperty<Control?> FrontContentProperty =AvaloniaProperty.Register<FlipPanel2, Control?>(nameof(FrontContent));public static readonly StyledProperty<Control?> BackContentProperty =AvaloniaProperty.Register<FlipPanel2, Control?>(nameof(BackContent));public static readonly StyledProperty<bool> IsFlippedProperty =AvaloniaProperty.Register<FlipPanel2, bool>(nameof(IsFlipped));public static readonly StyledProperty<double> FlipAngleProperty =AvaloniaProperty.Register<FlipPanel2, double>(nameof(FlipAngle), 0d);// ========== CLR 包装 ==========public Control? FrontContent{get => GetValue(FrontContentProperty);set => SetValue(FrontContentProperty, value);}public Control? BackContent{get => GetValue(BackContentProperty);set => SetValue(BackContentProperty, value);}public bool IsFlipped{get => GetValue(IsFlippedProperty);set => SetValue(IsFlippedProperty, value);}public double FlipAngle{get => GetValue(FlipAngleProperty);set => SetValue(FlipAngleProperty, value);}// ========== 模板部件 ==========private ToggleButton? flipButton;public FlipPanel2(){// 监听属性变化this.GetObservable(IsFlippedProperty).Subscribe(_ =>{UpdatePseudoClasses();});}protected override void OnApplyTemplate(TemplateAppliedEventArgs e){base.OnApplyTemplate(e);flipButton = e.NameScope.Find<ToggleButton>("PART_FlipButton");if (flipButton != null)flipButton.Click += (_, _) => ToggleFlipped();UpdatePseudoClasses();}private void ToggleFlipped(){IsFlipped = !IsFlipped;}private void UpdatePseudoClasses(){PseudoClasses.Set(":flipped", IsFlipped);}}
}

测试的Style.axaml

<Styles xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:AvaloniaUI"><Design.PreviewWith></Design.PreviewWith><Style Selector="local|FlipPanel2"><!-- 默认角度 --><Setter Property="local:FlipPanel2.FlipAngle" Value="0"/><Setter Property="Template"><Setter.Value><ControlTemplate><Grid RowDefinitions="*, auto"><!-- Front content --><Border x:Name="PART_FrontHost"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"CornerRadius="{TemplateBinding CornerRadius}"Background="#E0F7FA"Opacity="1"><ContentPresenter Content="{TemplateBinding FrontContent}"HorizontalAlignment="Center"VerticalAlignment="Center"/><Border.Transitions><Transitions><DoubleTransition Property="Opacity"Duration="0:0:0.3"Easing="SineEaseInOut"/></Transitions></Border.Transitions></Border><!-- Back content --><Border x:Name="PART_BackHost"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"CornerRadius="{TemplateBinding CornerRadius}"Background="#FFF3E0"Opacity="0"><ContentPresenter Content="{TemplateBinding BackContent}"HorizontalAlignment="Center"VerticalAlignment="Center"/><Border.Transitions><Transitions><DoubleTransition Property="Opacity"Duration="0:0:0.3"Easing="SineEaseInOut"/></Transitions></Border.Transitions></Border><!-- Flip button --><ToggleButton x:Name="PART_FlipButton"Grid.Row="1"Width="20" Height="20"Margin="0,5,0,5"HorizontalAlignment="Center"VerticalAlignment="Center"><ToggleButton.Template><ControlTemplate><Grid><Ellipse Stroke="#555" Fill="WhiteSmoke" StrokeThickness="0.5"/><Path Data="M4,5 L7,6.5 L4,8 Z" Margin="2,0,0,0"Fill="#455A64"Height="15"Stretch="Uniform"HorizontalAlignment="Center"VerticalAlignment="Center"/></Grid></ControlTemplate></ToggleButton.Template><ToggleButton.RenderTransform><Rotate3DTransform AngleY="{Binding FlipAngle, RelativeSource={RelativeSource TemplatedParent}}"><Rotate3DTransform.Transitions><Transitions><DoubleTransition Property="AngleY"Duration="0:0:0.3"Easing="SineEaseInOut"/></Transitions></Rotate3DTransform.Transitions></Rotate3DTransform></ToggleButton.RenderTransform></ToggleButton></Grid></ControlTemplate></Setter.Value></Setter><!-- flipped 状态样式 --><Style Selector="^:flipped /template/ Border#PART_FrontHost"><Setter Property="Opacity" Value="0"/><Setter Property="IsHitTestVisible" Value="False"/></Style><Style Selector="^:flipped /template/ Border#PART_BackHost"><Setter Property="Opacity" Value="1"/><Setter Property="IsHitTestVisible" Value="True"/></Style><Style Selector="^:not(:flipped) /template/ Border#PART_FrontHost"><Setter Property="IsHitTestVisible" Value="True"/></Style><Style Selector="^:not(:flipped) /template/ Border#PART_BackHost"><Setter Property="IsHitTestVisible" Value="False"/></Style><!-- 旋转角度 --><Style Selector="^:flipped"x:SetterTargetType="local:FlipPanel2"><Setter Property="local:FlipPanel2.FlipAngle" Value="180"/></Style></Style>
</Styles>

FlipPanelTest.axaml代码

<Window xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="using:AvaloniaUI"Height="300" Width="300"x:Class="AvaloniaUI.FlipPanelTest"Title="FlipPanelTest"><Grid Background="Transparent"><local:FlipPanel2 x:Name="panel"BorderBrush="DarkOrange"BorderThickness="3"IsFlipped="True"CornerRadius="4"Margin="10"><!-- Front content --><local:FlipPanel2.FrontContent><StackPanel Margin="6"><StackPanel.Styles><Style Selector="Button"><Setter Property="HorizontalAlignment" Value="Center"/><Setter Property="Padding" Value="3"/><Setter Property="Margin" Value="3"/></Style></StackPanel.Styles><TextBlock Text="This is the front side of the FlipPanel."TextWrapping="Wrap"Margin="3"FontSize="16"Foreground="DarkOrange"/><Button Content="Button One"/><Button Content="Button Two"/><Button Content="Button Three"/><Button Content="Button Four"/></StackPanel></local:FlipPanel2.FrontContent><!-- Back content --><local:FlipPanel2.BackContent><Grid Margin="6" RowDefinitions="auto,*"><TextBlock Text="This is the back side of the FlipPanel."TextWrapping="Wrap"Margin="3"FontSize="16"Foreground="DarkMagenta"/><Button Grid.Row="1"Margin="3"Padding="10"Content="Flip Back to Front"HorizontalAlignment="Center"VerticalAlignment="Center"Click="cmdFlip_Click"/></Grid></local:FlipPanel2.BackContent></local:FlipPanel2></Grid>
</Window>

FlipPanelTest.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Shares.Avalonia;namespace AvaloniaUI;public partial class FlipPanelTest : Window
{public FlipPanelTest(){InitializeComponent();this.Load("avares://AvaloniaUI/Demos/Book/18/CustomControls/FlipPanel.axaml");}private void cmdFlip_Click(object? sender, RoutedEventArgs e){// 切换翻转状态panel.IsFlipped = !panel.IsFlipped;}
}

运行效果

image

 

http://www.jsqmd.com/news/47678/

相关文章:

  • 2025 最新仿石漆厂家权威推荐榜:真石漆 / 绿色环保仿石漆优质品牌精选仿石漆/真石漆/绿色真石漆/有资质的仿石漆公司推荐
  • 2025年纱线烘干机制造厂权威推荐榜单:气流烘干机/筒子烘干机/快速烘干机源头制造厂精选
  • CTF逆向Re:零基础系统性入门教程-5-动态调试
  • CF1817B Fish Graph
  • CF1630C Paint the Middle
  • CF1707B Difference Array
  • P3113 [USACO14DEC] Marathon G
  • 封装map和set(红黑树作为底层结构如何完成map和set插入遍历)
  • 淮安市一对一辅导机构权威排行榜推荐:2026家教机构穿透式测评!
  • 崖山数据库导出 - 华
  • 南昌航空大学-软件学院-23207201-吕玉英
  • AI Compass前沿速览:Nano Banana Pro、Gemini 3 、 HunyuanVideo 1.5 、Meta SAM 3D生成
  • Prufer序列与Cayley公式
  • MX Round 27 解题报告
  • 11.22模拟赛
  • 从超时到秒杀:三路快排解决数组排序的完整实战与反思
  • 2025年光伏安装厂家权威推荐榜单:光伏施工/光伏/光伏发电源头厂家精选
  • 机房夸夸乐
  • 2025年镀锌水沟盖板订做厂家权威推荐榜单:雨水沟盖板/污水沟盖板/镀锌排水沟盖板源头厂家精选
  • 完整教程:【Deepseek OCR】重磅测试,mac环境下的体验【本人已经本地实验成功】
  • 使用C# Channel实现工位流水线调度系统
  • 2025年发电机制造厂权威推荐榜单:康姆勒原装发电机组/康姆勒发电机组/全自动柴油发电机组源头厂家精选
  • 2025百元白酒精选推荐指南:十大香型佳酿与纯粮酒挑选策略
  • BLOG1-NCHU-单部电梯调度程序
  • Hadoop生态系统怎样优化存储性能
  • 【matlab】机器学习入门之旅
  • web漏洞、waf繞過和前端加密繞過
  • 部署tendis 集群
  • P4555 [国家集训队] 最长双回文串 踢姐
  • 2025年水肥一体机制造厂权威推荐榜单:便携式水肥一体机/全自动喷淋系统/简易水肥一体源头厂家精选