类文件:
public class ImageButton:ContentControl
{
public ImageButton()
{
this.DefaultStyleKey=typeof(ImageButton);
}
}
Generic.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:MyImageButton">
<Style TargetType="custom:ImageButton">
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="custom:ImageButton">
<Grid x:Name="RootElement">
<Rectangle x:Name="BodyElement" Width="200" Height="100" Fill="Lavender" Stroke="Purple" RadiusX="16" RadiusY="16" />
<TextBlock Text="Clsick Me" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
测试页面:
<navigation:Page xmlns:my="clr-namespace:MyImageButton;assembly=MyImageButton" x:Class="SilverlightDemo.ImageButton.TestPage"
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"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="1024" d:DesignHeight="768"
Title="TestPage Page">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="91" />
<RowDefinition Height="677" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center">自定义Imagebutton控件</TextBlock>
<my:ImageButton Grid.Row="1" Width="200" Height="200">
</my:ImageButton>
</Grid>
</navigation:Page>
总是读取不了 样式 不知道是什么原因