【问题标题】:How to Enable/Disable button in wpf如何在 wpf 中启用/禁用按钮
【发布时间】:2021-05-18 21:51:38
【问题描述】:

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

编辑

<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 解决了你的问题吗?
  • @vlad - 我没有使用 MVVM
  • 你需要展示你的代码关于你到目前为止所尝试的内容。否则这里的人必须猜测你的实现并提供答案。
  • 两个按钮具有相同的图像。但我不想禁用外观。直接从 UI 中消失

标签: wpf


【解决方案1】:

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

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

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

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

希望对你有帮助

【讨论】:

  • 你能用一些 XAML 解释一下吗?
【解决方案2】:
<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>


您可以通过 xaml 禁用 Capture,然后通过在 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;
        }
    }
}

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

【讨论】:

  • 感谢您的精彩解释。但我在两个按钮上都有相同的图像。所以用户无法理解点击了哪个按钮。两个按钮都在同一个地方
  • 这正是我会做的,但有人可以解释为什么stackoverflow.com/questions/27555996/… 中的最佳答案强调“在你知道什么时候应该做之前,你不应该以编程方式改变 UI那”(即何时可以以编程方式使用BStart.IsEnabled?)
【解决方案3】:

您应该将当前状态存储在某个变量中(例如_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 文件中的任何位置。
  • 请解释一下。我想覆盖按钮。
【解决方案4】:

是的,我得到了答案。做一个简单的解决方案

btnhide.visibility= system.visibility. hidden;

【讨论】:

    【解决方案5】:

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

    1. .xaml 文件中
      使用IsEnabled="False"。如果您将 False 设置为 IsEnabled 属性,则默认情况下您的按钮将被禁用。
    2. .cs 文件中
      ButtonName.IsEnabled = false; 如果您将按钮的IsEnabled 属性设置为false,它将禁用该按钮

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 2012-12-16
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 2012-12-12
      • 2015-05-05
      相关资源
      最近更新 更多