Я хотел, когда я нажимаю на кнопку, это изменение фонового изображения на другое, и когда я отпускаю кнопку, она возвращается к исходному изображению. После многих исследований я нашел это tutorial, что помогло мне много и отлично работает! Но проблема в том, что я не могу отключить кнопку, она показывает, что она отключена, но если я нажму на нее, она работает так же, как она не была отключена! Вот часть кодаКнопка не отключена
using System;
using System.Windows;
using System.Windows.Media.Imaging;
namespace DotNetApp
{
public partial class ButtonImage
{
#region Fields
public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof (bool), typeof (ButtonImage), null);
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof (string), typeof (ButtonImage), null);
public static readonly DependencyProperty ImagePressedSourceProperty = DependencyProperty.Register("ImagePressedSource", typeof (string), typeof (ButtonImage), null);
public static readonly DependencyProperty ImageDisabledSourceProperty = DependencyProperty.Register("ImageDisabledSource", typeof (string), typeof (ButtonImage), null);
private BitmapImage _image;
private BitmapImage _imagePressed;
private BitmapImage _imageDisabled;
private bool _isPressed;
#endregion
#region Constructor
public ButtonImage()
{
InitializeComponent();
}
#endregion
#region Properties
public new bool IsEnabled
{
get { return (bool)GetValue(IsEnabledProperty); }
set
{
SetValue(IsEnabledProperty, value);
SetImageFromState();
}
}
public string ImageSource
{
get { return (string) GetValue(ImageSourceProperty); }
set
{
SetValue(ImageSourceProperty, value);
_image = SetImage(value);
SetImageFromState();
}
}
public string ImagePressedSource
{
get { return (string) GetValue(ImagePressedSourceProperty); }
set
{
SetValue(ImagePressedSourceProperty, value);
_imagePressed = SetImage(value);
SetImageFromState();
}
}
public string ImageDisabledSource
{
get { return (string) GetValue(ImageDisabledSourceProperty); }
set
{
SetValue(ImageDisabledSourceProperty, value);
_imageDisabled = SetImage(value);
SetImageFromState();
}
}
#endregion
#region Event Handlers
private void ButtonIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
SetImageFromState();
}
private void ButtonMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
_isPressed = true;
SetImageFromState();
}
private void ButtonMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
_isPressed = false;
SetImageFromState();
}
#endregion
#region Private Methods
private static BitmapImage SetImage(string imageSource)
{
BitmapImage bitmapImage = null;
if (!string.IsNullOrEmpty(imageSource))
{
bitmapImage = new BitmapImage(new Uri(imageSource, UriKind.RelativeOrAbsolute));
}
return bitmapImage;
}
private void SetImageFromState()
{
if (!IsEnabled)
{
image.Source = _imageDisabled;
}
else if (_isPressed)
{
image.Source = _imagePressed;
}
else
{
image.Source = _image;
}
}
#endregion
}
}
Вот XAML
<Tap_Test:ButtonImage ImageSource="/images/buttons/star2.png"
ImageDisabledSource="/images/buttons/star2lock.png"
ImagePressedSource="/images/buttons/star2-2.png"
Style="{StaticResource ButtonImageStyle}"
x:Name="star2" Margin="136,80,134,480" BorderBrush="{x:Null}" Click="star2_Click" IsEnabled="False"/>
Вы можете просмотреть полный код на ссылку я предоставил! Я также пробовал star2.IsEnable = false;
, и это не сработало. Какие-либо предложения? Благодаря!
Привет @KillaKem спасибо за помощь мне, но приходят с этой ошибкой App.ButtonImage.disableButtton.get»должен объявить тело, потому что оно не отмечено абстрактно, ехЬегп или частичного , Любая идея почему? –
Компилятор хочет тело для getter disableButton, проверьте, исправляет ли отредактированный код проблему. – KillaKem
О, я вижу, спасибо, но кнопка еще не отключена :(Я все еще могу щелкнуть по ней. –