按钮不会禁用

时间:2013-10-11 03:01:58

标签: c# windows-phone-8

我希望当我点击按钮时,背景图像会更改为其他图像,当我释放按钮时,会返回到原始图像。经过大量的研究,我发现了这个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;但它没有用。有什么建议?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为问题来自于“SetImageFromState()”方法总是运行的事实,你需要一种方法来阻止它运行,考虑一下:

private bool _disableButton;

public bool disableButton { 
                              get { return _disableButton; }
                              set { 
                                     _disableButton = value;
                                     SetValue(IsEnabledProperty, !_disableButton);           

                                  } 
                             }

public new bool IsEnabled
    {
        get { return (bool)GetValue(IsEnabledProperty); }

        set
        {
            if(!disableButton)
             {
                SetValue(IsEnabledProperty, value);
                SetImageFromState();
             }


        }
    }

因此,如果您想真正禁用按钮,那么

myButton.disableButton = true;