我想使用wpf制作自定义按钮。该按钮将用于控制媒体播放器。使用枚举
指定按钮的类型(播放,停止,暂停,...)namespace vgtunesWPF
{
public class MediaButton : Button
{
[Browsable(true)]
public Button_Type ButtonType
{
get { return (Button_Type)GetValue(ButtonTypeProperty); }
set { SetValue(ButtonTypeProperty, value); }
}
public static readonly DependencyProperty ButtonTypeProperty = DependencyProperty.Register("ButtonType", typeof(Button_Type), typeof(MediaButton), new FrameworkPropertyMetadata(Button_Type.stop, FrameworkPropertyMetadataOptions.AffectsRender, ImageSourceChanged));
static MediaButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MediaButton), new FrameworkPropertyMetadata(typeof(MediaButton)));
}
private static void ImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
Application.GetResourceStream(new Uri("pack://application:,,," + (string) e.NewValue));
}
}
public enum Button_Type
{
play = 0,
pause = 1,
stop = 2,
forward = 3,
backward = 4
}
public class ButtonTypeConverter : IValueConverter
{
public object Convert(object value, Type TargetType, object Parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
Button_Type type = (Button_Type)value;
switch (type)
{
case Button_Type.play:
return new BitmapImage(new Uri("/vgtunesWPF;component/Images/Play.png",UriKind.Relative));
case Button_Type.stop:
return new BitmapImage(new Uri("/vgtunesWPF;component/Images/Stop.png",UriKind.Relative));
default:
break;
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
为了将buttontype转换为右图,我创建了一个转换器。 那么XAML:
<Style TargetType="{x:Type local:MediaButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MediaButton}">
<Canvas Width="30" Height="30">
<Canvas.Resources>
<local:ButtonTypeConverter x:Key="Converter"/>
</Canvas.Resources>
<Image Name="Normal" Source="{Binding Source={StaticResource Converter}, Converter={StaticResource Converter},Path=}" />
<Image Name="Disabled" Visibility="Hidden" Source="/vgtunesWPF;component/Images/PlayDisabled.png"/>
</Canvas>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Effect">
<Setter.Value>
<DropShadowEffect Color="AliceBlue" ShadowDepth="0" BlurRadius="15" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Disabled" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这里的问题是我想将ButtonType绑定到普通图像,但我无法弄清楚如何。我无法在xaml
中访问ButtonType答案 0 :(得分:1)
好的解决了。显然我不得不添加相对来源
<Image Name="Normal" Source="{Binding Converter={StaticResource Converter}, Path=ButtonType, RelativeSource={RelativeSource TemplatedParent}}" />
答案 1 :(得分:0)
也许您尝试绑定到依赖项属性“ButtonType”。
<Image Name="Normal" Source="{Binding ButtonType, Converter={StaticResource Converter}"/>