머리말
프로젝트에 직접 사용하기 쉬운, 우리의 네 가지 상태 아이콘을 사용할 수 있도록이 프로그램은, 기사의 바닥이 소스 코드를 제공 할 것입니다, WPF 사용자 지정 컨트롤하여 ImageButton에 설계, 데모 시연 효과는 다음입니다 :
WPF 프로젝트
RBImageButton.cs
public class RBImageButton : Button
{
static RBImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(RBImageButton), new FrameworkPropertyMetadata(typeof(RBImageButton)));
}
#region 依赖属性
public double ImageSize
{
get { return (double)GetValue(ImageSizeProperty); }
set { SetValue(ImageSizeProperty, value); }
}
public static readonly DependencyProperty ImageSizeProperty = DependencyProperty.Register("ImageSize", typeof(double), typeof(RBImageButton),
new FrameworkPropertyMetadata(30.0, FrameworkPropertyMetadataOptions.AffectsRender));
public string ImagePath
{
get { return (string)GetValue(ImagePathProperty); }
set { SetValue(ImagePathProperty, value); }
}
public static readonly DependencyProperty ImagePathProperty = DependencyProperty.Register("ImagePath", typeof(string), typeof(RBImageButton),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender, ImageSourceChanged));
public Visibility BorderVisibility
{
get { return (Visibility)GetValue(BorderVisibilityProperty); }
set { SetValue(BorderVisibilityProperty, value); }
}
public static readonly DependencyProperty BorderVisibilityProperty =
DependencyProperty.Register("BorderVisibility", typeof(Visibility), typeof(RBImageButton),
new FrameworkPropertyMetadata(Visibility.Hidden, FrameworkPropertyMetadataOptions.AffectsRender));
#endregion 依赖属性
#region 事件
//依赖属性发生改变时候触发
private static void ImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
try
{
Application.GetResourceStream(new Uri("pack://application:,,," + (string)e.NewValue));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
}
}
#endregion 事件
}
테마 \ generic.xaml을
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RB.ToolKit">
<Style TargetType="{x:Type local:RBImageButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:RBImageButton}">
<Grid>
<StackPanel
Orientation="Vertical" VerticalAlignment="{TemplateBinding VerticalAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}">
<Rectangle x:Name="bgrect"
Height="{Binding ImageSize, RelativeSource={RelativeSource TemplatedParent}}"
Width="{Binding ImageSize, RelativeSource={RelativeSource TemplatedParent}}"
VerticalAlignment="Center" HorizontalAlignment="Center"
ToolTip="{TemplateBinding ToolTip}">
<Rectangle.Fill>
<ImageBrush ImageSource="{Binding ImagePath,RelativeSource={RelativeSource TemplatedParent}}" Stretch="Uniform" Viewbox="0,0,0.25,1" />
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="Center" Margin="{TemplateBinding Padding}"
VerticalAlignment="Center" RecognizesAccessKey="True" />
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bgrect" Property="Fill">
<Setter.Value>
<ImageBrush ImageSource="{Binding ImagePath,RelativeSource={RelativeSource TemplatedParent}}" Stretch="Uniform" Viewbox="0.25,0,0.25,1" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="bgrect" Property="Fill">
<Setter.Value>
<ImageBrush ImageSource="{Binding ImagePath,RelativeSource={RelativeSource TemplatedParent}}" Stretch="Uniform" Viewbox="0.5,0,0.25,1" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="bgrect" Property="Fill">
<Setter.Value>
<ImageBrush ImageSource="{Binding ImagePath,RelativeSource={RelativeSource TemplatedParent}}" Stretch="Uniform" Viewbox="0.75,0,0.25,1" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
기준 코드
<Window x:Class="ImageButtonDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="clr-namespace:RB.ToolKit;assembly=RB.ToolKit"
Title="ImageButton多态图标" Height="350" Width="525">
<Grid>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<toolkit:RBImageButton ImagePath="/ImageButtonDemo;component/Image/testsong.png" ImageSize="50" Content="正常" VerticalAlignment="Top" HorizontalAlignment="Center"/>
<Label Content="博客地址:http://blog.csdn.net/mybelief321"/>
<toolkit:RBImageButton ImagePath="/ImageButtonDemo;component/Image/testsong.png" ImageSize="50" Content="失能" VerticalAlignment="Top" HorizontalAlignment="Center" IsEnabled="False"/>
<Label Content="欢迎交流"/>
</StackPanel>
</Grid>
</Window>
완전한
/ ************************************************* ************************************************** ******
무단 전재를 들어 소스를 표시하시기 바랍니다 : HTTP : //blog.csdn.net/mybelief321/article/details/49999147
************************************************** ************************************************** ***** /