我有一个播放按钮,我想更改背景图像,所以它在点击时看起来像一个暂停按钮,我已经看到很多指南如何做到这一点但是我的椭圆模板似乎使我的代码与任何东西有很大不同我研究过。这是我的代码:
<Button HorizontalAlignment="Left" Width="40" Height="40" Click="Button_Click_2">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Stroke="Black">
<Ellipse.Fill>
<ImageBrush ImageSource="Images/play.png"/>
</Ellipse.Fill>
</Ellipse>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
答案 0 :(得分:2)
ToggleButton解决方案(在其他用户的回答中建议)可能最适合您。不过,为了完整起见,我发布了另一种方法。
XAML:
<Window x:Class="WpfTestBench.PlayButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfTestBench="clr-namespace:WpfTestBench"
Title="Play button sample">
<Button Width="40" Height="40" Click="Button_OnClick">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Stroke="Black" />
<Image Source="Play.png" Visibility="{Binding Path=IsPlaying,
Converter={wpfTestBench:BoolToVisibilityConverter},
ConverterParameter={x:Static Visibility.Hidden}}" />
<Image Source="Pause.png" Visibility="{Binding Path=IsPlaying,
Converter={wpfTestBench:BoolToVisibilityConverter},
ConverterParameter={x:Static Visibility.Visible}}" />
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
代码隐藏:
using System.ComponentModel;
using System.Windows;
namespace WpfTestBench
{
public partial class PlayButton
{
public PlayButton()
{
InitializeComponent();
DataContext = new SampleContext();
}
private void Button_OnClick(object sender, RoutedEventArgs e)
{
var context = DataContext as SampleContext;
if (context == null)
return;
context.IsPlaying = !context.IsPlaying;
}
}
public class SampleContext : INotifyPropertyChanged
{
private bool _isPlaying;
public bool IsPlaying
{
get { return _isPlaying; }
set
{
if (_isPlaying == value)
return;
_isPlaying = value;
OnPropertyChanged("IsPlaying");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
转换器:
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace WpfTestBench
{
public class BoolToVisibilityConverter : MarkupExtension, IValueConverter
{
private static BoolToVisibilityConverter _instance;
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var visibility = Visibility.Hidden;
if (parameter != null)
visibility = (Visibility)parameter;
return visibility == Visibility.Visible
? (((bool)value) ? Visibility.Visible : Visibility.Hidden)
: (((bool)value) ? Visibility.Hidden : Visibility.Visible);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
#endregion
public override object ProvideValue(IServiceProvider serviceProvider)
{
return _instance ?? (_instance = new BoolToVisibilityConverter());
}
}
}
答案 1 :(得分:0)
我认为你应该使用ToggleButton(一个具有已检查和未选中状态的按钮),并且有一个触发器,当选中该按钮时,它会更改其模板(或只是背景的imagesource)。