如何在WPF中启用/禁用按钮

11

我有两个按钮,开始和捕获。我希望在表单加载时禁用捕获按钮并启用开始按钮,当点击开始后禁用开始按钮并启用捕获按钮。

请帮忙,谢谢。

<Grid>
    <Button Content="Button" Height="77" HorizontalAlignment="Left" Margin="92,151,0,0" Name="button1" VerticalAlignment="Top" Width="109">
        <Button.Background>
            <ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
        </Button.Background>
    </Button>
    <Button Content="Button" Height="77" HorizontalAlignment="Left" Margin="229,151,0,0" Name="button2" VerticalAlignment="Top" Width="112" IsEnabled="False" >
        <Button.Background>
            <ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
        </Button.Background>
    </Button>
</Grid>

@user3721563解决了您的问题吗? - Masoud Mohammadi
@vlad - 我没有使用MVVM。 - Dipika
您需要展示您已尝试过的代码。否则这里的人们将不得不猜测您的实现并提供答案。 - Krishna
两个按钮都有相同的图像。但我不想让它看起来被禁用,而是直接从用户界面消失。 - Dipika
5个回答

25
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="BStart" Content="Start" Width="100" Height="50" HorizontalAlignment="Left" Click="BStart_Click" />
        <Button Name="BCapture" Content="Capture" Width="100" Height="50" HorizontalAlignment="Right" IsEnabled="False" />
    </Grid>
</Window>

这里输入图片描述
你可以通过在C#中编写代码来禁用XAML捕获,然后再启用它。


你可以通过在C#中编写代码来禁用XAML捕获,然后再启用它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BStart_Click(object sender, RoutedEventArgs e)
        {
            BStart.IsEnabled = false;
            BCapture.IsEnabled = true;
        }
    }
}

使用IsEnabled属性来启用和禁用,使用Click事件来点击按钮。


感谢您的出色解释。但是我在两个按钮上有同样的图像。因此,用户无法理解点击了哪个按钮。两个按钮都在同一位置。 - Dipika
这正是我会做的方式,但有人能否解释一下为什么 https://dev59.com/LYbca4cB1Zd3GeqPSzVB 的顶部答案强调“在你知道应该这样做之前,不应该以编程方式更改UI”(即何时可以以编程方式使用 BStart.IsEnabled )? - Yu Chen

2

将 IsEnabled="False" 设置为禁用按钮

<Button Name="btn" IsEnabled="False" Content="press"/>

将IsEnabled属性设置为“True”以启用按钮

<Button Name="btn" IsEnabled="True" Content="press"/>

希望这能帮到您。

你能用一些XAML来解释一下吗? - Frebin Francis

1

是的,我得到了答案。只需提供简单的解决方案。

btnhide.visibility= system.visibility. hidden;

1
你应该将当前状态存储在某个变量中(例如 _capturing),当变量发生改变时,刷新 IsEnabled 属性。
Xaml 代码:
<Button Content="Start" Name="StartButton" Grid.Row="0" Click="StartButton_Click" />
<Button Content="Capture" Name="CaptureButton" Grid.Row="1" Click="CaptureButton_Click"/>

C# 代码:

public partial class MainWindow : Window
{
    private bool _capturing;

    public MainWindow()
    {
        InitializeComponent();

        // Some code

        _capturing = false;
        UpdateButtons();
    }

    private void StartButton_Click(object sender, RoutedEventArgs e)
    {
        // Some code

        _capturing = true;
        UpdateButtons();
    }

    private void CaptureButton_Click(object sender, RoutedEventArgs e)
    {
        // Some code

        UpdateButtons();
    }

    private void UpdateButtons()
    {
        StartButton.IsEnabled = !_capturing;
        CaptureButton.IsEnabled = _capturing;
    }
}

更新

你需要添加点击事件处理程序,然后你的XAML代码就可以工作:

<Button Click="StartButton_Click" Content="Button" Height="77" HorizontalAlignment="Left" Margin="92,151,0,0" Name="button1" VerticalAlignment="Top" Width="109">
    <Button.Background>
        <ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
    </Button.Background>
</Button>
<Button Click="CaptureButton_Click" Content="Button" Height="77" HorizontalAlignment="Left" Margin="229,151,0,0" Name="button2" VerticalAlignment="Top" Width="112" IsEnabled="False" >
    <Button.Background>
        <ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
    </Button.Background>
</Button>


是的,您可以将按钮替换为XAML文件中的任何位置。 - Vlad
请问能否解释一下,我想要覆盖按钮。 - Dipika

0

在C# WPF中禁用按钮有两种方法。

  1. .xaml文件中
    使用IsEnabled="False"。如果将False设置为IsEnabled属性,则您的按钮将默认处于禁用状态。
  2. .cs文件中
    ButtonName.IsEnabled = false; 如果将IsEnabled属性设置为false,则会禁用该按钮。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接