0
我创建自定义的控制不能结合的图像源 - 图像按钮。目的是让按钮没有任何背景或边框,只有3个图像(正常状态,按下和鼠标悬停)。使用它时 - 所有3个图像应该是有界的。我从here得到一些代码,但是源代码是永久的。我的代码是某事像那:自定义控件三态的ImageButton,
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:JoesControls">
<Style TargetType="{x:Type local:ImageButton}" x:Key="styledImageButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ImageButton}">
<Grid
Margin="{TemplateBinding Control.Padding}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
>
<Image Name="Normal" Source="{TemplateBinding NormalSource}"/>
<Image Name="Pressed" Source="{TemplateBinding PressedSource}" Visibility="Hidden"/>
<Image Name="Over" Source="{TemplateBinding MouseOverSource}" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Over" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
而且ImageButton.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace JoesControls
{
public class ImageButton : Button
{
static ImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
}
#region NormalSourceProperty
public static readonly DependencyProperty NormalSourceProperty =
DependencyProperty.Register("NormalSource", typeof(ImageSource), typeof(ImageButton),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsParentMeasure));
public ImageSource NormalSource
{
get { return (ImageSource)GetValue(NormalSourceProperty); }
set
{
SetValue(NormalSourceProperty, value);
}
}
#endregion // NormalSource
#region PressedSourceProperty
public static readonly DependencyProperty PressedSourceProperty =
DependencyProperty.Register("PressedSource", typeof(ImageSource), typeof(ImageButton),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsParentMeasure));
public ImageSource PressedSource
{
get
{
if (PressedSource == null || PressedSource.ToString() == String.Empty)
SetValue(PressedSourceProperty, NormalSource);
return (ImageSource)GetValue(PressedSourceProperty);
}
set
{
SetValue(PressedSourceProperty, value);
}
}
#endregion // PressedSource
#region MouseOverSourceProperty
public static readonly DependencyProperty MouseOverSourceProperty =
DependencyProperty.Register("MouseOverSource", typeof(ImageSource), typeof(ImageButton),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsParentMeasure));
public ImageSource MouseOverSource
{
get
{
if (MouseOverSource == null || MouseOverSource.ToString() == String.Empty)
SetValue(MouseOverSourceProperty, NormalSource);
return (ImageSource)GetValue(MouseOverSourceProperty);
}
set
{
SetValue(MouseOverSourceProperty, value);
}
}
#endregion // OverSource
}
}
它编译没有错误,但显然是行不通的。来源不受限制。我猜源绑定设置不正确,但我一直在努力了一段时间,google搜索,但我stucked。我感谢任何帮助。
感谢
将是很好的发布一些评论,以帮助别人,我明白了为什么这是一个不好回答 – Joe 2014-09-04 13:36:12