0
我想當我點擊一個按鈕是背景圖像更改爲其他人,當我釋放按鈕回到他的原始圖像。經過大量的研究,我發現這個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」必須聲明主體,因爲它不標記爲抽象,EXTERN或部分。任何想法爲什麼? –
編譯器需要disableButton的getter的主體,檢查編輯的代碼是否修復問題。 – KillaKem
哦,我看到了,謝謝,但仍然沒有禁用按鈕:(我仍然可以點擊它。 –