0

我想当我单击一个按钮时将背景图像更改为另一个,当我释放按钮时返回到他的原始图像。经过大量研究后,我发现了本 教程,它对我有很大帮助并且效果很好!但问题是我无法禁用该按钮,它显示它已被禁用,但如果我点击它,它就像它没有被禁用一样工作!这是代码的一部分

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;了,但没有用。有什么建议么?谢谢!

4

1 回答 1

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;
于 2013-10-11T06:36:05.747 回答