WPF学习(一):自定义控件
1、如何防止将拖动窗体至显示器最上沿时,窗体不自己变大:
设置Window的ResizeMode属性为NoResize;
2、如何设置窗体无边框:
设置窗体的WindowStyle属性为None;
3、如何设置窗体背景为透明:
AllowTransparency="True" Background="{x:Null}"
4、如何设置Boder的四个圆角不相同:
CornerRadius="0, 10, 0, 0"
5、如何设置控件资源模板:
1)可以在任意窗口控件下定义XX.Resources
例:
<Window.Resources></Window.Resources>
<Border.Resources></Border.Resources>
2)在上述资源中定义ControlTemplate:
例:
<ControlTemplate>
</ControlTemplate>
3)在ControlTemplate属性中定义控件类型TargetType和控件名x:Key:
例:
<ControlTemplate TargetType="Button" x:Key="CloseButtonTemplate">
</ControlTemplate>
3)在上述ControlTemplate中嵌套定义相关东西,比如要在Button中定义一下Border,在Border中画一个叉叉:
<ControlTemplate TargetType="Button" x:Key="CloseButtonTemplate">
<Border x:Name="ButtonBackground" Background="Transparent">
<Path Data="M0 0 14 14M0 14 14 0" Stroke="Black" StrokeThickness="2"></Path>
</Border>
</ControlTemplate>
6、如何定义触发器:
在上述资源模板中,如果不进行触发器定义,当鼠标移动上去或者点击下去不动的时候,是不会产生任何明显的不同的,是故需要定义触发器。触发器的定义如下:
<ControlTemplate.Triggers>
定义Triggers和Setter
</ControlTemplate.Triggers>
例:
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonBackground" Property="Background" Value="#22FFFFFF"></Setter>
</Trigger>
</ControlTemplate.Triggers>
7、如何引用资源模板:
直接在控件的属性Template中引用。例:
Template="{StaticResource ResourceKey=CloseButtonTemplate}"
8、全实例:
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
WindowStyle="None" AllowsTransparency="True" Background="{x:Null}" WindowStartupLocation="CenterScreen">
<Window.Resources>
<ControlTemplate TargetType="Button" x:Key="One">
<Border x:Name="backone" CornerRadius="100 0 0 0" Background="Aqua"></Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="backone" Property="Background" Value="Aquamarine"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="backone" Property="Background" Value="Red"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate TargetType="Button" x:Key="Two">
<Border x:Name="backtwo" CornerRadius="0 100 0 0 " Background="Aqua"></Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="backtwo" Property="Background" Value="Brown"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="backtwo" Property="Background" Value="Black"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate TargetType="Button" x:Key="Three">
<Border x:Name="backthree" CornerRadius="0 0 0 100" Background="Azure"></Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="backthree" Property="Background" Value="Coral"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="backthree" Property="Background" Value="Blue"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate TargetType="Button" x:Key="Four">
<Border CornerRadius="0 0 100 0" x:Name="backfour" Background="Magenta"></Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="backfour" Property="Background" Value="Green"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="backfour" Property="Background" Value="Lime"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<UniformGrid VerticalAlignment="Center" HorizontalAlignment="Center" Height="200" Width="200">
<Button x:Name="FirstButton" Template="{StaticResource One}"></Button>
<Button x:Name="SecondButton" Template="{StaticResource Two}"></Button>
<Button x:Name="ThirdButton" Template="{StaticResource Three}"></Button>
<Button x:Name="Colse" Click="Colse_OnClick" Template="{StaticResource Four}"/>
</UniformGrid>
</Grid>
</Window>