如何启用/禁用 wpf 中的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28338223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:21:54  来源:igfitidea点击:

How to Enable/Disable button in wpf

wpf

提问by Dipika

I Have 2 button , Start And capture. I want disable capture button on form load and enable start. and on after click on start disable start button and enable capture. Please help me. Thanks in advance

我有 2 个按钮,开始和捕获。我想在表单加载时禁用捕获按钮并启用启动。然后单击开始禁用开始按钮并启用捕获。请帮我。提前致谢

Edit

编辑

<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>

回答by Masoud Mohammadi

<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>

enter image description here
you can disable Capture by xaml and then enable it by writing code in c#.

在此处输入图片说明
您可以通过 xaml 禁用捕获,然后通过在 c# 中编写代码来启用它。

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;
        }
    }
}

for enable and disable use IsEnabled Property. and for clicking on the button use Click event.

启用和禁用使用 IsEnabled 属性。并单击按钮使用 Click 事件。

回答by Frebin Francis

Set IsEnabled="False" to disable the button

设置 IsEnabled="False" 以禁用按钮

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

Set IsEnabled="True" to enable the button

设置 IsEnabled="True" 以启用按钮

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

Hope this helps

希望这可以帮助

回答by Vlad

You should store current state in some variable (e.g. _capturing). When variable changing, you refresh IsEnabledproperty.

您应该将当前状态存储在某个变量中(例如_capturing)。当变量改变时,你刷新IsEnabled属性。

Xaml code:

Xml代码:

<Button Content="Start" Name="StartButton" Grid.Row="0" Click="StartButton_Click" />
<Button Content="Capture" Name="CaptureButton" Grid.Row="1" Click="CaptureButton_Click"/>

C# code:

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;
    }
}

UPDATE

更新

You should add click handler and your xaml code will work:

您应该添加点击处理程序,您的 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>

回答by Dipika

Yes I got my answer. Just make simeple solution

对,我有答案了。只做简单的解决方案

btnhide.visibility= system.visibility. hidden;

回答by Nikunj Savani

There are two ways to disable a button In C# WPF.

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

  1. In .xamlFile
    Using IsEnabled="False". If you set False to IsEnabledattribute, then your button will be disabled by default.
  2. In .csFile
    ButtonName.IsEnabled = false;If you set IsEnabledproperty as false for the button, it will disable the button
  1. .xaml文件中
    使用IsEnabled="False". 如果您将 False 设置为IsEnabled属性,则默认情况下您的按钮将被禁用。
  2. .cs文件中
    ButtonName.IsEnabled = false;如果您将IsEnabled按钮的属性设置为 false,它将禁用该按钮