ButtonBase.IsPressed недоступен в WPF - PullRequest
0 голосов
/ 09 марта 2020

Я пытался изменить размер кнопки при нажатии кнопки, и я использовал события «Click» и «MouseLeave», но я не хочу изменять размер кнопки без нажатия кнопки. Я использовал ButtonBase.Ispressed для создания этой функции, но я получил сообщение об ошибке: ButtonBase.IsPressed не может быть использован, потому что установленный метод доступа недоступен. Пожалуйста, помогите мне получить доступ к этому набору доступа. Спасибо. Вот код:

   public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }



    private void SearchMedicationButton_OnClick(object sender, RoutedEventArgs e)
    {
        double _width = 436;
        double _height = 593;

        DoubleAnimation widthAnimation = new DoubleAnimation(_width,_width*0.8,new Duration(timeSpan:TimeSpan.FromSeconds(0.2)));
        DoubleAnimation heightAnimation = new DoubleAnimation(_height,_height*0.8,new Duration(timeSpan:TimeSpan.FromSeconds(0.2)));

        SearchMedicationButton.BeginAnimation(Button.WidthProperty, widthAnimation);
        SearchMedicationButton.BeginAnimation(Button.HeightProperty,heightAnimation);
    }

    private void SearchMedicationButton_OnMouseLeave(object sender, MouseEventArgs e)
    {
        double _width = 436;
        double _height = 593;

        DoubleAnimation widthAnimation = new DoubleAnimation(_width*0.8, _width , new Duration(timeSpan: TimeSpan.FromSeconds(0.2)));
        DoubleAnimation heightAnimation = new DoubleAnimation(_height*0.8, _height, new Duration(timeSpan: TimeSpan.FromSeconds(0.2)));

        SearchMedicationButton.BeginAnimation(Button.WidthProperty, widthAnimation);
        SearchMedicationButton.BeginAnimation(Button.HeightProperty, heightAnimation);
    }

1 Ответ

0 голосов
/ 09 марта 2020

Если вы ищете что-то вроде ниже -

enter image description here

Вот правильный способ сделать это ...

XAML UI

<Button x:Name="Helllll" 
            Content="Hello" 
            Height="30" Width="60"/> 

C# код для MainWindow

public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;

            Helllll.MouseEnter += new MouseEventHandler(Helllll_MouseEnter);
            Helllll.MouseLeave += new MouseEventHandler(Helllll_MouseLeave);
        }

        void Helllll_MouseLeave(object sender, MouseEventArgs e)
        {
            Helllll.Height = 30;
            Helllll.Width = 60;
        }

        void Helllll_MouseEnter(object sender, MouseEventArgs e)
        {
            Helllll.Height = 60;
            Helllll.Width = 100;
        }

Дайте мне знать, если это работает для вас.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...