12

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>
4
  • @user3721563 did it solve your problem? Commented Feb 5, 2015 at 7:37
  • @vlad - I did not use MVVM Commented Feb 5, 2015 at 7:39
  • You need to show your code about what you tried so far. Otherwise people here have to guess your implementation and provide answers. Commented Feb 5, 2015 at 9:06
  • both button having same image. but I dont want disable look. directly disappear from UI Commented Feb 5, 2015 at 9:16

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks For Such great explanation. But I have same image on both button. So the user cannot understand about which button were click. Both button are on same place
This is exactly how I would do it too, but could someone please explain why the top answer in stackoverflow.com/questions/27555996/… emphatically emphasizes "you shouldn't be programmatically altering the UI until you know when you should do that" (ie. when is it okay to programmatically is BStart.IsEnabled?)
2

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

1 Comment

could you please explain with some XAML ?
1

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

Xaml code:

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

C# code:

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:

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

2 Comments

Yes. You can replace buttons to any place in xaml file.
Will u please explain. I want to override buttons.
1

Yes I got my answer. Just make simeple solution

btnhide.visibility= system.visibility. hidden;

Comments

0

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

  1. In .xaml File
    Using IsEnabled="False". If you set False to IsEnabled attribute, then your button will be disabled by default.
  2. In .cs File
    ButtonName.IsEnabled = false; If you set IsEnabled property as false for the button, it will disable the button

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.