wpf3d游戏引擎前端ControlTemplate实现

1.ControlTemplates.xaml

<ResourceDictionary

x:Class="PrimalEditor.Dictionaries.ControlTemplates"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="clr-namespace:PrimalEditor.Dictionaries"

xmlns:utl="clr-namespace:PrimalEditor.Utilities.Controls"

xmlns:wpf="http://schemas.microsoft.com/netfx/2009/xaml/presentation">

<!-- LightTextBlockStyle: Style for TextBlock -->

<Style TargetType="{x:Type TextBlock}" x:Key="LightTextBlockStyle">

<Setter Property="Foreground" Value="{StaticResource Editor.FontBrush}"/>

<Setter Property="VerticalAlignment" Value="Center"/>

<Setter Property="HorizontalAlignment" Value="Left"/>

</Style>

<!-- LightTextBoxStyle: Style for TextBox with specific background and borders -->

<Style TargetType="{x:Type TextBox}" x:Key="LightTextBoxStyle">

<Setter Property="Foreground" Value="{StaticResource Editor.FontBrush}"/>

<Setter Property="Background" Value="{StaticResource Editor.Window.GrayBrush2}"/>

<Setter Property="BorderBrush" Value="{StaticResource Editor.Window.GrayBrush4}"/>

<Setter Property="SelectionBrush" Value="White"/>

<Setter Property="VerticalAlignment" Value="Center"/>

<Setter Property="HorizontalAlignment" Value="Stretch"/>

<Setter Property="Padding" Value="0,2"/>

</Style>

<!-- TextBoxStyle_WithRename: Extended style with KeyDown and LostFocus handlers -->

<Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyle_WithRename" BasedOn="{StaticResource LightTextBoxStyle}">

<EventSetter Event="KeyDown" Handler="OnTextBoxRename_KeyDown"/>

<EventSetter Event="LostFocus" Handler="OnTextBoxRename_LostFocus"/>

</Style>

<!-- FocusVisual: Focus visual for controls like buttons -->

<Style x:Key="FocusVisual">

<Setter Property="Control.Template">

<Setter.Value>

<ControlTemplate>

<Rectangle Margin="2" StrokeDashArray="1 2" SnapsToDevicePixels="true" StrokeThickness="1" Stroke="{StaticResource Editor.FontBrush}"/>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

<!-- ButtonStyle: Style for Button with gradient background and border -->

<Style TargetType="{x:Type Button}">

<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>

<Setter Property="Background">

<Setter.Value>

<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">

<GradientStop Color="{StaticResource Editor.Window.GrayColor5}" Offset="0"/>

<GradientStop Color="{StaticResource Editor.Window.GrayColor4}" Offset="1"/>

</LinearGradientBrush>

</Setter.Value>

</Setter>

<Setter Property="BorderBrush">

<Setter.Value>

<LinearGradientBrush>

<GradientStop Color="{StaticResource Editor.Window.GrayColor5}" Offset="0"/>

<GradientStop Color="{StaticResource Editor.Window.GrayColor2}" Offset="1"/>

</LinearGradientBrush>

</Setter.Value>

</Setter>

<Setter Property="Foreground" Value="{StaticResource Editor.FontBrush}"/>

<Setter Property="BorderThickness" Value="0,1"/>

<Setter Property="HorizontalContentAlignment" Value="Center"/>

<Setter Property="VerticalContentAlignment" Value="Center"/>

<Setter Property="Padding" Value="5,3"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type Button}">

<Grid>

<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true" CornerRadius="3"/>

<Border x:Name="borderSelected" SnapsToDevicePixels="true" CornerRadius="3" BorderThickness="0" BorderBrush="Transparent"/>

<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

</Grid>

<ControlTemplate.Triggers>

<Trigger Property="IsDefaulted" Value="true">

<Setter Property="Background" TargetName="borderSelected" Value="{StaticResource Editor.Selected.BackgroundBrush}"/>

<Setter Property="OpacityMask" TargetName="borderSelected" Value="{StaticResource Button.Selected.OpacityBrush}"/>

</Trigger>

<Trigger Property="IsMouseOver" Value="true">

<Setter Property="TextBlock.Foreground" TargetName="contentPresenter" Value="White" />

<Setter Property="Background" TargetName="borderSelected" Value="{StaticResource Editor.Selected.BackgroundBrush}"/>

<Setter Property="OpacityMask" TargetName="borderSelected" Value="{StaticResource Button.Selected.OpacityBrush}"/>

</Trigger>

<Trigger Property="IsPressed" Value="true">

<Setter Property="TextBlock.Foreground" TargetName="contentPresenter" Value="White" />

<Setter Property="Background" TargetName="borderSelected" Value="{StaticResource Editor.Selected.BackgroundBrush}"/>

<Setter Property="OpacityMask" TargetName="borderSelected" Value="{StaticResource Button.Pressed.OpacityBrush}"/>

</Trigger>

<Trigger Property="IsEnabled" Value="false">

<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Editor.Disabled.FontBrush}" />

<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.BackgroundBrush}"/>

<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.BorderBrush}"/>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

<!-- ListBoxItemStyle: Style for ListBoxItem with selection and mouseover effects -->

<Style TargetType="{x:Type ListBoxItem}" x:Key="ListBoxItemStyle">

<Setter Property="SnapsToDevicePixels" Value="True" />

<Setter Property="Padding" Value="4,2,0,2" />

<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>

<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>

<Setter Property="Background" Value="Transparent" />

<Setter Property="BorderBrush" Value="Transparent" />

<Setter Property="BorderThickness" Value="0" />

<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type ListBoxItem}">

<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">

<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

</Border>

<ControlTemplate.Triggers>

<MultiTrigger>

<MultiTrigger.Conditions>

<Condition Property="Selector.IsSelectionActive" Value="False" />

<Condition Property="IsSelected" Value="True" />

</MultiTrigger.Conditions>

<Setter TargetName="Bd" Property="Background" Value="{StaticResource Editor.Window.GrayBrush4}" />

</MultiTrigger>

<MultiTrigger>

<MultiTrigger.Conditions>

<Condition Property="Selector.IsSelectionActive" Value="True" />

<Condition Property="IsSelected" Value="True" />

</MultiTrigger.Conditions>

<Setter TargetName="Bd" Property="Background" Value="{StaticResource Editor.Selected.BackgroundBrush}" />

</MultiTrigger>

<MultiTrigger>

<MultiTrigger.Conditions>

<Condition Property="IsMouseOver" Value="True" />

<Condition Property="Selector.IsSelectionActive" Value="True" />

<Condition Property="IsSelected" Value="False"/>

</MultiTrigger.Conditions>

<Setter TargetName="Bd" Property="Background">

<Setter.Value>

<SolidColorBrush Color="{StaticResource Editor.Selected.BackgroundColor}" Opacity="0.5"/>

</Setter.Value>

</Setter>

</MultiTrigger>

<MultiTrigger>

<MultiTrigger.Conditions>

<Condition Property="IsMouseOver" Value="True" />

<Condition Property="Selector.IsSelectionActive" Value="False" />

<Condition Property="IsSelected" Value="False"/>

</MultiTrigger.Conditions>

<Setter TargetName="Bd" Property="Background" Value="{StaticResource Editor.Window.GrayBrush4}" />

</MultiTrigger>

<Trigger Property="IsEnabled" Value="False">

<Setter TargetName="Bd" Property="TextElement.Foreground" Value="{StaticResource Editor.Disabled.FontBrush}"/>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</ResourceDictionary>

相关推荐
掘金安东尼18 分钟前
前端周刊第421期(2025年7月1日–7月6日)
前端·面试·github
摸鱼仙人~20 分钟前
深入理解 classnames:React 动态类名管理的最佳实践
前端·react.js·前端框架
未来之窗软件服务23 分钟前
chrome webdrive异常处理-session not created falled opening key——仙盟创梦IDE
前端·人工智能·chrome·仙盟创梦ide·东方仙盟·数据调式
kymjs张涛23 分钟前
零一开源|前沿技术周报 #6
前端·ios·harmonyos
玲小珑26 分钟前
Next.js 教程系列(十)getStaticPaths 与动态路由的静态生成
前端·next.js
天天鸭33 分钟前
写个vite插件自动处理系统权限,降低99%重复工作
前端·javascript·vite
蓝婷儿37 分钟前
每天一个前端小知识 Day 23 - PWA 渐进式 Web 应用开发
前端
无奈何杨1 小时前
CoolGuard风控中新增移动距离和移动速度指标
前端·后端
恋猫de小郭1 小时前
Google I/O Extended :2025 Flutter 的现状与未来
android·前端·flutter
江城开朗的豌豆1 小时前
Vue-router方法大全:让页面跳转随心所欲!
前端·javascript·vue.js