将以往的代码复制到代码库

This commit is contained in:
于智纯
2025-08-30 17:19:57 +08:00
parent da46e0242d
commit 20ea70bf64
198 changed files with 10075 additions and 0 deletions

9
Example/App.xaml Normal file
View File

@@ -0,0 +1,9 @@
<Application x:Class="Example.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Example"
StartupUri="Custom4Behavior.xaml">
<Application.Resources>
</Application.Resources>
</Application>

17
Example/App.xaml.cs Normal file
View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace Example
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

10
Example/AssemblyInfo.cs Normal file
View File

@@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@@ -0,0 +1,21 @@
<Window x:Class="Example.Custom4Behavior"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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="clr-namespace:Example"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
mc:Ignorable="d"
Title="Custom4Behavior" Height="450" Width="800" Background="Silver" Foreground="White">
<b:Interaction.Behaviors>
<local:CustomTitleBar TitleHeight="40" TitleBar="{Binding ElementName=TitleBar, Mode=OneWay}" HoverBrush="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" Title="窗口行为测试" Header="中心标题测试"/>
</b:Interaction.Behaviors>
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="TitleBar" Grid.Row="0"/>
<Border x:Name="ClientArea" Grid.Row="1"/>
</Grid>
</Window>

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Example
{
/// <summary>
/// Custom4Behavior.xaml 的交互逻辑
/// </summary>
public partial class Custom4Behavior : Window
{
public Custom4Behavior()
{
InitializeComponent();
}
}
}

62
Example/CustomWindow.xaml Normal file
View File

@@ -0,0 +1,62 @@
<Window x:Class="Example.CustomWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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="clr-namespace:Example"
mc:Ignorable="d"
Title="CustomWindow" Height="450" Width="800" Background="Gray" WindowStyle="ToolWindow">
<Window.Resources>
<Viewbox x:Key="MinButton">
<Path Fill="White" Height="1024" Width="1024" Data="M130 545.3h766c17.7 0 32-14.3 32-32s-14.3-32-32-32H130c-17.7 0-32 14.3-32 32 0 17.6 14.3 32 32 32z"/>
</Viewbox>
<Viewbox x:Key="CloseButton">
<Path Fill="White" Height="1024" Width="1024" Data="M548.992 503.744L885.44 167.328a31.968 31.968 0 1 0-45.248-45.248L503.744 458.496 167.328 122.08a31.968 31.968 0 1 0-45.248 45.248l336.416 336.416L122.08 840.16a31.968 31.968 0 1 0 45.248 45.248l336.416-336.416L840.16 885.44a31.968 31.968 0 1 0 45.248-45.248L548.992 503.744z"/>
</Viewbox>
<Viewbox x:Key="MaxButton">
<Path Fill="White" Height="1024" Width="1024" Data="M959.72 0H294.216a63.96 63.96 0 0 0-63.96 63.96v127.92H64.28A63.96 63.96 0 0 0 0.32 255.84V959.4a63.96 63.96 0 0 0 63.96 63.96h703.56a63.96 63.96 0 0 0 63.96-63.96V792.465h127.92a63.96 63.96 0 0 0 63.96-63.96V63.96A63.96 63.96 0 0 0 959.72 0zM767.84 728.505V959.4H64.28V255.84h703.56z m189.322 0H831.8V255.84a63.96 63.96 0 0 0-63.96-63.96H294.216V63.96H959.72z"/>
</Viewbox>
<Viewbox x:Key="TitleIcon">
<Path Fill="Yellow" Width="1024" Height="1024">
<Path.Data>
<GeometryGroup>
<PathGeometry Figures="M844.318261 412.021885c-9.195123-27.108025-28.565176-78.309281-52.60816-109.562649a26.806546 26.806546 0 0 0-26.128217-10.049314c-68.938296 12.787752-354.740793 76.098432-405.037612 254.574253a26.60556 26.60556 0 0 0 6.255699 25.650875c9.471479 10.049314 29.168135 21.756765 70.093967 25.927231a28.791285 28.791285 0 0 0 2.939424 0.150739c18.515862-0.15074 198.473957-5.602493 395.490763-156.769302a26.756299 26.756299 0 0 0 8.994136-29.921833z m-4.597561 22.78682c0 0.854192-0.401973 1.105425-1.105425 1.582767-186.490149 125.139086-322.507618 144.082043-322.507618 144.082043-107.000074 13.466081-128.128757-24.84693-130.314482-51.427366a49.24164 49.24164 0 0 1 10.551779-34.318408c117.074511-150.488481 353.760985-194.228121 375.392135-197.946368a2.512329 2.512329 0 0 1 2.512328 1.080302c51.15101 72.606296 61.049584 122.099168 62.355995 130.038126a6.858657 6.858657 0 0 0 1.582767 3.090164l0.351726 1.105425c0.301479 0.703452 1.205918 1.281288 1.205918 2.160603z"/>
<PathGeometry Figures="M558.214284 476.764592m-41.026326 0a41.026325 41.026325 0 1 0 82.052651 0 41.026325 41.026325 0 1 0-82.052651 0Z"/>
<PathGeometry Figures="M739.529037 348.736328a36.378518 36.378518 0 1 0 36.378517 36.378518 36.378518 36.378518 0 0 0-36.378517-36.378518z m-0.175863 57.783557a20.977944 20.977944 0 1 1 20.977943-20.977943 20.977944 20.977944 0 0 1-20.977943 20.927697z"/>
<PathGeometry Figures="M745.181776 543.064943s-105.115827-378.884271-462.921662-246.2082c0-0.025123 302.861209-110.944429 462.921662 246.2082zM819.898427 547.93886c-142.800756 153.252042-302.18288 88.710322-323.185946 79.238843a0.854192 0.854192 0 0 0-1.080302 1.205918c49.040654 89.187664 127.676538 98.608896 158.653549 99.061115a71.601364 71.601364 0 0 0 23.364656-3.567506c145.539194-47.2569 145.564317-156.36733 144.333276-175.209795a1.180794 1.180794 0 0 0-2.085233-0.728575z m-136.042591 159.432371s-98.156677 32.409038-147.699797-55.798818a1.030055 1.030055 0 0 1 1.306411-1.432027c19.922766 8.742903 142.44903 55.472215 253.192473-60.923968a2.788685 2.788685 0 0 1 4.748301 1.532521c2.788685 20.299615 3.51726 83.158076-111.547388 116.622292z"/>
<PathGeometry Figures="M125.239579 608.661842a101.045855 81.851665 90 1 0 163.703329 0 101.045855 81.851665 90 1 0-163.703329 0Z"/>
<PathGeometry Figures="M517.162835 168.552123S198.750313 162.422042 200.40845 500.38048l-41.754901 17.410437S165.813685 156.844672 517.162835 168.552123zM222.140092 709.707696s125.264702 173.702397 350.670821 140.313551c0 0 272.261047-45.046051 287.284771-342.380137 0.07537 0-151.920508 554.496038-637.955592 202.066586z"/>
<PathGeometry Figures="M935.365048 266.256581m-45.071174 0a45.071174 45.071174 0 1 0 90.142349 0 45.071174 45.071174 0 1 0-90.142349 0Z"/>
<PathGeometry Figures="M255.704801 928.380775m-45.071174 0a45.071174 45.071174 0 1 0 90.142349 0 45.071174 45.071174 0 1 0-90.142349 0Z"/>
</GeometryGroup>
</Path.Data>
</Path>
</Viewbox>
</Window.Resources>
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="40"/>
</WindowChrome.WindowChrome>
<Grid Margin="8">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
<RowDefinition Height="8"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="Gray">
<Grid HorizontalAlignment="Left" Width="auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<ContentControl Content="{StaticResource TitleIcon}" Margin="2"/>
</Grid>
<TextBlock HorizontalAlignment="Center" Text="自动化测试系统" Foreground="White" VerticalAlignment="Center"/>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True">
<Button Height="auto" Width="{Binding RelativeSource={RelativeSource Mode=Self}}" Background="Transparent" BorderBrush="Transparent" Content="{StaticResource MinButton}" Padding="6"/>
<Button Height="auto" Width="{Binding RelativeSource={RelativeSource Mode=Self}}" Background="Transparent" BorderBrush="Transparent" Content="{StaticResource MaxButton}" Padding="6"/>
<Button Height="auto" Width="{Binding RelativeSource={RelativeSource Mode=Self}}" Background="Transparent" BorderBrush="Transparent" Content="{StaticResource CloseButton}" Padding="6"/>
</StackPanel>
</Grid>
<Button Grid.Row="1" BorderBrush="Red">测试</Button>
</Grid>
</Window>

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Example
{
/// <summary>
/// CustomWindow.xaml 的交互逻辑
/// </summary>
public partial class CustomWindow : Window
{
public CustomWindow()
{
InitializeComponent();
}
}
}

View File

@@ -0,0 +1,318 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shell;
using Microsoft.Xaml.Behaviors;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Input;
namespace Example
{
/// <summary>
/// 为窗口提供自定义标题栏的行为
/// </summary>
public class CustomTitleBar : Behavior<Window>
{
//private readonly static string svgMax = "M959.72 0H294.216a63.96 63.96 0 0 0-63.96 63.96v127.92H64.28A63.96 63.96 0 0 0 0.32 255.84V959.4a63.96 63.96 0 0 0 63.96 63.96h703.56a63.96 63.96 0 0 0 63.96-63.96V792.465h127.92a63.96 63.96 0 0 0 63.96-63.96V63.96A63.96 63.96 0 0 0 959.72 0zM767.84 728.505V959.4H64.28V255.84h703.56z m189.322 0H831.8V255.84a63.96 63.96 0 0 0-63.96-63.96H294.216V63.96H959.72z";
//private readonly static string[] svgNomal = new string[] {
// "M785.960424 410.377773c0-14.634419-11.871277-26.608035-26.608035-26.608035L204.677194 383.769738c-14.634419 0-26.608035 11.871277-26.608035 26.608035l0 349.998001c0 14.634419 11.871277 26.608035 26.608035 26.608035l554.675195 0c14.634419 0 26.608035-11.871277 26.608035-26.608035L785.960424 410.377773zM740.931441 741.954827 223.098141 741.954827 223.098141 428.798721l517.8333 0L740.931441 741.954827z",
// "M856.471717 294.735159l-554.675195 0c-12.485309 0-22.514491 10.029182-22.514491 22.514491s10.029182 22.514491 22.514491 22.514491L838.153108 339.764142l0 331.679392c0 12.485309 10.029182 22.514491 22.514491 22.514491s22.514491-10.029182 22.514491-22.514491l0-349.998001C883.182091 306.708775 871.106136 294.735159 856.471717 294.735159z"
//};
//private readonly static Geometry svgMaxGeometry;
//private readonly static Geometry svgNomalGeometry;
//static CustomTitleBar()
//{
// svgMaxGeometry = Geometry.Parse(svgMax);
// GeometryGroup nomal = new GeometryGroup();
// nomal.Children.Add(Geometry.Parse(svgNomal[0]));
// nomal.Children.Add(Geometry.Parse(svgNomal[1]));
// svgNomalGeometry = nomal;
//}
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
Grid titleBarRoot = new Grid() { Height = this.TitleHeight };
titleBarRoot.Background = this.Background ?? this.AssociatedObject.Background;
StackPanel spLeft = new StackPanel() { VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Left, Orientation = Orientation.Horizontal };
Border bIcon = new Border() { Width = this.TitleHeight };
bIcon.Child = this.TitleIcon ?? new Image() { Source = this.AssociatedObject.Icon };
TextBlock tbTitle = new TextBlock()
{
Text = this.Title,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Left,
Foreground = Foreground = this.Foreground ?? this.AssociatedObject.Foreground,
FontSize = this.AssociatedObject.FontSize
};
spLeft.Children.Add(bIcon);
spLeft.Children.Add(tbTitle);
string templateString = "" +
"<ResourceDictionary xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"\r\n" +
" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">\r\n" +
" <ControlTemplate x:Key=\"dpButtonTemplate\" TargetType=\"Button\">\r\n" +
" <Border BorderBrush=\"{TemplateBinding BorderBrush}\" Background=\"{TemplateBinding Background}\" BorderThickness=\"{TemplateBinding BorderThickness}\">\r\n" +
" <ContentPresenter Margin=\"{TemplateBinding Padding}\"/>\r\n" +
" </Border>\r\n" +
" </ControlTemplate>\r\n" +
"</ResourceDictionary>\r\n";
ResourceDictionary dictionary = (ResourceDictionary)XamlReader.Parse(templateString);
ControlTemplate template = (ControlTemplate)dictionary["dpButtonTemplate"] as ControlTemplate;
StackPanel spRight = new StackPanel() { VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Right, Orientation = Orientation.Horizontal, Width = this.TitleHeight * 4, FlowDirection = FlowDirection.RightToLeft };
Button butClose = new Button()
{
Name = "Close",
Background = this.Background ?? this.AssociatedObject.Background,
Foreground = this.Foreground ?? this.AssociatedObject.Foreground,
BorderThickness = new Thickness(0),
Padding = new Thickness(10),
Template = template,
Content = new Viewbox()
{
Child = new Path()
{
Data = Geometry.Parse("M548.992 503.744L885.44 167.328a31.968 31.968 0 1 0-45.248-45.248L503.744 458.496 167.328 122.08a31.968 31.968 0 1 0-45.248 45.248l336.416 336.416L122.08 840.16a31.968 31.968 0 1 0 45.248 45.248l336.416-336.416L840.16 885.44a31.968 31.968 0 1 0 45.248-45.248L548.992 503.744z"),
Height = 1024,
Width = 1024,
Fill = this.AssociatedObject.Foreground
}
}
};
Button butMax = new Button()
{
Name = "Max",
Background = this.Background ?? this.AssociatedObject.Background,
Foreground = this.Foreground ?? this.AssociatedObject.Foreground,
BorderThickness = new Thickness(0),
Padding = new Thickness(10),
Template = template,
Content = new Viewbox()
{
Child = new Path()
{
Data = Geometry.Parse("M959.72 0H294.216a63.96 63.96 0 0 0-63.96 63.96v127.92H64.28A63.96 63.96 0 0 0 0.32 255.84V959.4a63.96 63.96 0 0 0 63.96 63.96h703.56a63.96 63.96 0 0 0 63.96-63.96V792.465h127.92a63.96 63.96 0 0 0 63.96-63.96V63.96A63.96 63.96 0 0 0 959.72 0zM767.84 728.505V959.4H64.28V255.84h703.56z m189.322 0H831.8V255.84a63.96 63.96 0 0 0-63.96-63.96H294.216V63.96H959.72z"),
Height = 1024,
Width = 1024,
Fill = this.AssociatedObject.Foreground
}
}
};
Button butMin = new Button()
{
Name = "Min",
Background = this.Background ?? this.AssociatedObject.Background,
Foreground = this.Foreground ?? this.AssociatedObject.Foreground,
BorderThickness = new Thickness(0),
Padding = new Thickness(10),
Template = template,
Content = new Viewbox()
{
Child = new Path()
{
Data = Geometry.Parse("M130 545.3h766c17.7 0 32-14.3 32-32s-14.3-32-32-32H130c-17.7 0-32 14.3-32 32 0 17.6 14.3 32 32 32z"),
Height = 1024,
Width = 1024,
Fill = this.AssociatedObject.Foreground
}
}
};
butClose.Click += ButClose_Click;
butMax.Click += ButMax_Click;
butMin.Click += ButMin_Click;
spRight.Children.Add(butClose);
spRight.Children.Add(butMax);
spRight.Children.Add(butMin);
butClose.AddHandler(Button.MouseEnterEvent, new RoutedEventHandler(this.ButMouseEnter));
butMax.AddHandler(Button.MouseEnterEvent, new RoutedEventHandler(this.ButMouseEnter));
butMin.AddHandler(Button.MouseEnterEvent, new RoutedEventHandler(this.ButMouseEnter));
butClose.AddHandler(Button.MouseLeaveEvent, new RoutedEventHandler(this.ButMouseLeave));
butMax.AddHandler(Button.MouseLeaveEvent, new RoutedEventHandler(this.ButMouseLeave));
butMin.AddHandler(Button.MouseLeaveEvent, new RoutedEventHandler(this.ButMouseLeave));
titleBarRoot.Children.Add(spLeft);
titleBarRoot.Children.Add(spRight);
titleBarRoot.Children.Add(new TextBlock()
{
Text = this.Header,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
Foreground = this.Foreground ?? this.AssociatedObject.Foreground
});
WindowChrome.SetIsHitTestVisibleInChrome(spRight, true);
this.TitleBar.Child = titleBarRoot;
}
private void ButMouseEnter(object sender, RoutedEventArgs e)
{
Button? button = sender as Button;
if (button != null) button.Background = this.HoverBrush;
}
private void ButMouseLeave(object sender, RoutedEventArgs e)
{
Button? button = sender as Button;
if (button != null) button.Background = new SolidColorBrush(Colors.Transparent);
}
private void ButMin_Click(object sender, RoutedEventArgs e)
{
this.AssociatedObject.WindowState = WindowState.Minimized;
}
private void ButMax_Click(object sender, RoutedEventArgs e)
{
if (this.AssociatedObject.WindowState == WindowState.Maximized)
this.AssociatedObject.WindowState = WindowState.Normal;
else
this.AssociatedObject.WindowState = WindowState.Maximized;
}
private void ButClose_Click(object sender, RoutedEventArgs e)
{
this.AssociatedObject.Close();
}
protected override void OnAttached()
{
/*
* <WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="40"/>
</WindowChrome.WindowChrome>
*/
Window target = this.AssociatedObject;
WindowChrome.SetWindowChrome(target, new WindowChrome() { CaptionHeight = this.TitleHeight });
this.AssociatedObject.Loaded += AssociatedObject_Loaded;
this.AssociatedObject.StateChanged += AssociatedObject_StateChanged;
}
private void AssociatedObject_StateChanged(object? sender, EventArgs e)
{
FrameworkElement? winContent = this.AssociatedObject.Content as FrameworkElement;
if (winContent == null) return;
if (this.AssociatedObject.WindowState == WindowState.Maximized)
winContent.Margin = new Thickness(8);
else winContent.Margin = new Thickness(0);
}
protected override void OnDetaching()
{
this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
}
public int TitleHeight
{
get { return (int)GetValue(TitleHeightProperty); }
set { SetValue(TitleHeightProperty, value); }
}
// Using a DependencyProperty as the backing store for TitleHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleHeightProperty =
DependencyProperty.Register("TitleHeight", typeof(int), typeof(CustomTitleBar), new PropertyMetadata(40));
public Decorator TitleBar
{
get { return (Decorator)GetValue(TitleBarProperty); }
set { SetValue(TitleBarProperty, value); }
}
// Using a DependencyProperty as the backing store for TitleBar. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleBarProperty =
DependencyProperty.Register("TitleBar", typeof(Decorator), typeof(CustomTitleBar), new PropertyMetadata(null));
public Brush Background
{
get { return (Brush)GetValue(BackgroundProperty); }
set { SetValue(BackgroundProperty, value); }
}
// Using a DependencyProperty as the backing store for Background. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackgroundProperty =
DependencyProperty.Register("Background", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(null));
public Brush HoverBrush
{
get { return (Brush)GetValue(HoverBrushProperty); }
set { SetValue(HoverBrushProperty, value); }
}
// Using a DependencyProperty as the backing store for HoverBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HoverBrushProperty =
DependencyProperty.Register("HoverBrush", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(null));
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
// Using a DependencyProperty as the backing store for Foreground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ForegroundProperty =
DependencyProperty.Register("Foreground", typeof(Brush), typeof(CustomTitleBar), new PropertyMetadata(null));
public UIElement TitleIcon
{
get { return (UIElement)GetValue(TitleIconProperty); }
set { SetValue(TitleIconProperty, value); }
}
// Using a DependencyProperty as the backing store for TitleIcon. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleIconProperty =
DependencyProperty.Register("TitleIcon", typeof(UIElement), typeof(CustomTitleBar), new PropertyMetadata(null));
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
// Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(CustomTitleBar), new PropertyMetadata(""));
public string Header
{
get { return (string)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
// Using a DependencyProperty as the backing store for Header. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(string), typeof(CustomTitleBar), new PropertyMetadata(""));
}
}

11
Example/Example.csproj Normal file
View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows7.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.122" />
</ItemGroup>
</Project>

18
Example/MainWindow.xaml Normal file
View File

@@ -0,0 +1,18 @@
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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="clr-namespace:Example"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
<Button Content="路由命令" HorizontalAlignment="Left" Margin="167,76,0,0" VerticalAlignment="Top" Command="{x:Static local:MainWindow.GetButtonContentCmd}" CommandTarget="{Binding ElementName=comboBox, Mode=OneWay}"/>
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="367,76,0,0" VerticalAlignment="Top" Width="120"/>
<Button x:Name="butCustom" Content="自定义命令" HorizontalAlignment="Left" Margin="264,246,0,0" VerticalAlignment="Top" Command="{Binding textCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" CommandParameter="{Binding ElementName=param, Mode=OneWay}"/>
<Label x:Name="param" Content="parameter" HorizontalAlignment="Left" Margin="461,158,0,0" VerticalAlignment="Top"/>
<Label x:Name="target" Content="target" HorizontalAlignment="Left" Margin="501,265,0,0" VerticalAlignment="Top"/>
<Button Content="测试窗体样式" HorizontalAlignment="Left" Margin="319,333,0,0" VerticalAlignment="Top" Click="Button_Click"/>
</Grid>
</Window>

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Example
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CommandBinding cbinding = new CommandBinding(MainWindow.GetButtonContentCmd);
cbinding.Executed += Cbinding_Executed;
cbinding.CanExecute += Cbinding_CanExecute;
this.CommandBindings.Add(cbinding);
this.textCommand.OnCanExecute = (obj) => true;
this.textCommand.OnExecute += TextCommand_OnExecute;
this.butCustom.Command = this.textCommand;
}
private void TextCommand_OnExecute(object? obj)
{
MessageBox.Show("自这义命令被调用!");
}
private void Cbinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void Cbinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(e.Source.ToString());
}
public static RoutedCommand GetButtonContentCmd = new RoutedCommand();
public CustomCommand textCommand = new CustomCommand();
public class CustomCommand : ICommand
{
public string? Command { get; set; }
public CustomCommand() { }
public CustomCommand(string command, Action<object?>? onExecute = null, Func<object?, bool>? onCanExecute = null)
{
this.Command = command;
this.OnExecute = onExecute;
this.OnCanExecute = onCanExecute;
}
public event EventHandler? CanExecuteChanged;
public event Action<object?>? OnExecute;
public Func<object?, bool>? OnCanExecute;
public bool CanExecute(object? parameter)
{
if (this.OnCanExecute != null) return this.OnCanExecute(parameter);
else if (this.Execute != null) return true;
else
{
this.CanExecuteChanged?.Invoke(this, new EventArgs());
return false;
}
}
public void Execute(object? parameter)
{
this.OnExecute?.Invoke(parameter);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
new CustomWindow().Show();
}
}
}