创建一个新的类(CustomButton)
public class CustomButton:Button
{
//通过这个方法,可以定义新的属性,在xmal中使用
//依赖属性
//propdp
//按钮圆角的属性
public CornerRadius ButtonCornerRadius
{
get { return (CornerRadius)GetValue(ButtonCornerRadiusProperty); }
set { SetValue(ButtonCornerRadiusProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonCornerRadiusProperty =
DependencyProperty.Register("ButtonCornerRadius", typeof(CornerRadius), typeof(CustomButton));
// 对触发器的颜色进行修改
public Brush BackgroundHover
{
get { return (Brush)GetValue(BackgroundHoverProperty); }
set { SetValue(BackgroundHoverProperty, value); }
}
// Using a DependencyProperty as the backing store for BackgroundHover. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackgroundHoverProperty =
DependencyProperty.Register("BackgroundHover", typeof(Brush), typeof(CustomButton));
public Brush BackgroundPressed
{
get { return (Brush)GetValue(BackgroundPressedProperty); }
set { SetValue(BackgroundPressedProperty, value); }
}
// Using a DependencyProperty as the backing store for BackgroundPressed. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackgroundPressedProperty =
DependencyProperty.Register("BackgroundPressed", typeof(Brush), typeof(CustomButton));
}
定义一个资源字典
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
//引入命名空间,方便访问控件
xmlns:bb="clr-namespace:WPF_LoginUI">
<Style TargetType="{x:Type bb:CustomButton}"> //这句话是定义新的按钮所必须的
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type bb:CustomButton}">
<Border x:Name="buttonBoder" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding ButtonCornerRadius}" > <!--这样就可以绑定background,可以直接在v里写background-->
<TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<!--触发器-->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="buttonBoder" Property="Background" Value="{Binding BackgroundHover,RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="buttonBoder" Property="Background" Value="{Binding BackgroundPressed,RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在App.xaml引用资源字典
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
//引入资源字典
<ResourceDictionary Source="CustomButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
最后在v里使用
<local:CustomButton ButtonCornerRadius="10" BackgroundHover="Red" BackgroundPressed="Green" Foreground="#FFFFFF" Background="#3C7FF8" Grid.Row="3" Grid.ColumnSpan="2" Content="登录" Command="{Binding LoginAction}"/>
创建一个新的类CustomTextBox
创建一个新的资源字典CustomTextboxStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:bb ="clr-namespace:WPF_LoginUI">
<Style TargetType="{x:Type bb:CustomTextBox}">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type bb:CustomTextBox}">
<!--这里就可以开始写内容了-->
<Border Background="{TemplateBinding Background}">
<!--必须要写,作用是可以在textbox上写入-->
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在App.xmal里引用项目
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CustomButtonStyle.xaml"/>
<ResourceDictionary Source="CustomTextboxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
不支持绑定的控件可以添加附加属性
属于依赖属性的一种
xmal
<PasswordBox Grid.Row="1" Grid.Column="1"
local:PasswordBoxHelper.IsEnableBind="True"
local:PasswordBoxHelper.MyPassword="{Binding Password}"/>
创建一个新的类(PasswordBoxHelper.cs)
public class PasswordBoxHelper
{
// 附加属性 快捷
// propa
public static string GetMyPassword(DependencyObject obj)
{
return (string)obj.GetValue(MyPasswordProperty);
}
public static void SetMyPassword(DependencyObject obj, string value)
{
obj.SetValue(MyPasswordProperty, value);
}
// Using a DependencyProperty as the backing store for MyPassword. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPasswordProperty =
DependencyProperty.RegisterAttached("MyPassword", typeof(string), typeof(PasswordBoxHelper));
public static bool GetIsEnableBind(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnableBindProperty);
}
public static void SetIsEnableBind(DependencyObject obj, bool value)
{
obj.SetValue(IsEnableBindProperty, value);
}
// Using a DependencyProperty as the backing store for IsEnableBind. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsEnableBindProperty =
DependencyProperty.RegisterAttached("IsEnableBind", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false,OnPasswordPropertyChanged));
private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PasswordBox pwd = d as PasswordBox;
if (pwd == null)
return;
if ((bool)e.OldValue)
pwd.PasswordChanged -= OnPwdProChaned;
if ((bool)e.NewValue)
pwd.PasswordChanged += OnPwdProChaned;
}
//去同步一下
private static void OnPwdProChaned(object sender, RoutedEventArgs e)
{
PasswordBox pwd = sender as PasswordBox;
SetMyPassword(pwd, pwd.Password);
}
}
自定义控件模板
<Window.Resources>
<ControlTemplate x:Key="btntemp">
<Grid >
<Ellipse Name="faceEllipse" Height="50" Width="100" Fill="{TemplateBinding Button.Background}"/>
<TextBlock Name="txtBlock" Text="text" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid >
<ControlTemplate.Triggers >
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="blue"/>
</Trigger >
</ControlTemplate.Triggers >
</ControlTemplate >
</Window.Resources>
<Canvas>
<Button x:Name="btn1" Click="Button_Click" Template="{StaticResource btntemp}"></Button>
</Canvas>
能实现以上相同功能,假如有多个按钮的话,则可以对模板进行复用,如果我们想当前窗体所有的按钮都统一样式,则可以指定
<ControlTemplate x:Key=“btntemp” TargetType=“Button”>