demo的xaml代码
<Window x:Class="WpfApp2.Login"
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="Login" Height="300" Width="500" BorderBrush="#FFABADB3" Background="#FFFFE7E7">
<Window.Resources>
<Style x:Key="myBtn" TargetType="{x:Type local:AttachProperty}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:AttachProperty}" >
<Grid>
<Ellipse Fill="{TemplateBinding Background}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{TemplateBinding Text1}" FontSize="{TemplateBinding Text1FontSize}"/>
<TextBlock Text="{TemplateBinding Text2}" FontSize="{TemplateBinding FontSize}"/>
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="Red"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="button"/>
</Window.Triggers>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup"/>
</VisualStateManager.VisualStateGroups>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Height="30" Width="300" Margin="20" Background="#FFFFE7E7">
</TextBox>
<PasswordBox Grid.Row="1" Height="30" Width="300" Margin="20" Background="#FFFFE7E7" BorderBrush="#FFABADB3"></PasswordBox>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<local:AttachProperty x:Name="bt1" Width="120" Grid.Column="0" Text1FontSize="30" Text2="测试" Text1="取消" FontSize="20" Style="{StaticResource myBtn}" Background="#FFEEB7B7" Content="取消" HorizontalAlignment="Left" Margin="73,17,0,13"/>
<Button x:Name="button" Height="20" Width="100" Grid.Column="1" Command="" Background="#FFEEB7B7" BorderBrush="#FF707070">确定
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
<TextBlock HorizontalAlignment="Left" Margin="26,38,0,0" TextWrapping="Wrap" Text="账号" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" Margin="26,38,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF0E0E0E"><Run Text="密码"/><InlineUIContainer>
</InlineUIContainer></TextBlock>
<DatePicker HorizontalAlignment="Left" Margin="198,33,0,0" Grid.Row="2" VerticalAlignment="Top"/>
</Grid>
</Window>
自定义控件附加属性的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp2
{
public class AttachProperty:Button
{
public static readonly DependencyProperty Text1Property = DependencyProperty.RegisterAttached("Text1", typeof(string), typeof(AttachProperty));
public string Text1
{
get { return (string)GetValue(Text1Property); }
set
{
SetValue(Text1Property, value);
}
}
public static readonly DependencyProperty Text2Property = DependencyProperty.RegisterAttached("Text2", typeof(string), typeof(AttachProperty));
public string Text2
{
get { return (string)GetValue(Text2Property); }
set
{
SetValue(Text2Property, value);
}
}
public static readonly DependencyProperty Text1FontSizeProperty = DependencyProperty.RegisterAttached("Text1FontSize", typeof(double), typeof(AttachProperty));
public double Text1FontSize
{
get { return (double)GetValue(Text1FontSizeProperty); }
set
{
SetValue(Text1FontSizeProperty, value);
}
}
}
}
初次接触记录一下,对于自定义控件里的属性,想要通过templatebinding绑定的话,在附加属性代码里类型要一致,否则会无效。这里重写了一个button,有两个textblock,可通过外部改一些属性。