Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Enable/Disable button in wpf

Tags:

wpf

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

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>
like image 379
Dipika Avatar asked Feb 05 '15 07:02

Dipika


2 Answers

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

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.

like image 51
Masoud Mohammadi Avatar answered Nov 16 '22 05:11

Masoud Mohammadi


Set IsEnabled="False" to disable the button

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

Set IsEnabled="True" to enable the button

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

Hope this helps

like image 2
Frebin Francis Avatar answered Nov 16 '22 07:11

Frebin Francis