ZC:踩坑过程在 后面
1、在 Generic.xaml 中添加内容
<ResourceDictionary
x:Class="WpfCustomControlLibrary_DateTimePicker.Themes.Generic" <!--ZC: 如果有事件触发的话,需要加这一句-->
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControlLibrary_DateTimePicker">
<Style TargetType="{x:Type local:DateTimePickerZ}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DateTimePickerZ}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<!-- ZC: 这里面的部分是 自己添加的,外层的是 原来的 -->
<!-- ZC: Calendar 貌似要到 .net4.0才有 ? -->
<StackPanel>
<Calendar x:Name="calendar1" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="1" BorderBrush="Black"/>
<Border x:Name="border1" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="1" BorderBrush="Black" Background="Transparent" Margin="0,-3,0,0" >
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Hour, Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}"
MinWidth="20" MaxLength="2" InputMethod.IsInputMethodEnabled="False" VerticalAlignment="Center" Background="Transparent"
TextWrapping="NoWrap" FontSize="12" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" BorderBrush="Transparent" />
<Label VerticalAlignment="Center" Content=":" FontSize="12" />
<TextBox Text="{Binding Minute, Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}"
MinWidth="20" MaxLength="2" InputMethod.IsInputMethodEnabled="False" VerticalAlignment="Center" Background="Transparent"
TextWrapping="NoWrap" FontSize="12" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" BorderBrush="Transparent"/>
<Label VerticalAlignment="Center" Content=":" FontSize="12" />
<TextBox Text="{Binding Second, Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}"
MinWidth="20" MaxLength="2" InputMethod.IsInputMethodEnabled="False" VerticalAlignment="Center" Background="Transparent"
TextWrapping="NoWrap" FontSize="12" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" BorderBrush="Transparent"/>
</StackPanel>
<Grid VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="50*"></RowDefinition>
<RowDefinition Height="50*"></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" VerticalAlignment="Stretch" Width="30" x:Name="btnUp">
<Path HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 8 L 5 0 L 10 8 Z">
<Path.Fill>
<SolidColorBrush Color="Black" />
</Path.Fill>
</Path>
</Button>
<Button Grid.Row="1" VerticalAlignment="Stretch" Width="30" Click="Click_Down">
<Path HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 5 8 L 10 0 Z" Margin="0,1,0,0">
<Path.Fill>
<SolidColorBrush Color="Black" />
</Path.Fill>
</Path>
</Button>
</Grid>
<Button Margin="0,0,0,0" VerticalAlignment="Stretch" Width="36" BorderBrush="Black">
<Path HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 8 L 5 0 L 10 8" Margin="2,4,0,0" Stroke="Black" StrokeThickness="4" />
</Button>
</StackPanel>
</Border>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
2、在 Themes文件夹中添加 类Generic.xaml.cs (创建的 Generic.xaml.cs 和 Generic.xaml 的路径关系如下面的图中显示的那样)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace WpfCustomControlLibrary_DateTimePicker.Themes
{
partial class Generic// ZC: 与Generic.xaml关联的话,需要加上关键字"partial"
{
void Click_Down(object sender, RoutedEventArgs e)
{
Console.WriteLine("Click_Down(...)");
MessageBox.Show("Click_Down(...)");
}
}// class
}
2.1、
2.2、
3、ZC:之前 不知道 怎么在Generic.xaml里面弄 类似 按钮事件的代码。查到说,添加类 写事件代码,然后要在 Generic.xaml中的ResourceDictionary节点添加 "x:Class"属性,老是提示 错误(忘了具体是什么了...),然后还 又添加了 一个别的什么属性,但是 还是出错,搞不定。搜来搜去也找不到太多关于这个的知识点...
3.1、找到了这一篇文章:ControlTemplate的ResourceDictionary中的WPF事件 - 代码日志.html(https://codeday.me/bug/20180705/189758.html)
ResourceDictionary可以像Windows等一样在后面的代码,所以你可以添加一个事件处理程序,并从那里调用DragMove
设置后面的代码需要几个步骤。
>如果您的ResourceDictionary被称为MetroStyleResourceDictionary.xaml,则可以在Visual Studio中的同一文件夹中添加一个名为MetroStyleResourceDictionary.xaml.cs的新文件
>文件后面的代码应该是这样的
public partial class MetroStyleResourceDictionary
{
//...
}
>之后,您需要将x:Class属性添加到Xaml文件
<ResourceDictionary x:Class="YourNamespace.MetroStyleResourceDictionary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--...-->
</ResourceDictionary>
现在,您可以为MouseLeftButtonDown的dragRectangle添加一个事件处理程序。您还需要抓住“窗口”,使其成为一个很好的主意
<Rectangle Name="dragRectangle"
MouseLeftButtonDown="dragRectangle_MouseLeftButtonDown"
Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
.../>
最后,您可以将事件处理程序添加到文件后面的代码中,如下所示
public partial class MetroStyleResourceDictionary
{
void dragRectangle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Rectangle dragRectangle = sender as Rectangle;
Window window = dragRectangle.Tag as Window;
if (window != null)
{
window.DragMove();
}
}
}
ZC:终于知道 自己之前为什么失败了... 原来 创建的类的 类文件名 需要与 xaml文件有对应关系,如我的 xaml文件名为"Generic.xaml" 则创建的 类的文件名就应该是"Generic.xaml.cs",然后再在 "x:Class"中 指明对应的类
4、
5、