TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

WPF IsEnabled Property

This WPF example uses the IsEnabled property with the Button control. It sets IsEnabled in a C# event handler.

IsEnabled is a bool property. It is available on many controls in WPF, including the TextBox and Button.

When IsEnabled is False, a control is made inactive—it cannot be clicked or used. We use it with an expression.

Bool

Example. This example has many parts to it. First, I added a TextBox and a Button to the Window, which nests the controls within a Grid. On the TextBox, I changed the Text to be empty. On the Button, I set IsEnabled to false.

TextBoxButton

TextChanged: On the TextBox I added a TextChanged event handler. Press tab after typing TextChanged in the XAML.

Name: On the Button, I added a Name attribute. This makes the Button easy to access in C# code by the identifier "SendButton."

Name

You will need to drag the controls around so that they are positioned in a nice way. The Grid control, which is nested by default in the Window, makes this much easier. Then we add code to the TextBox_TextChanged event handler.

Grid

Example markup: XAML

<Window x:Class="WpfApplication12.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>
	<TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0"
		 TextWrapping="Wrap" Text=""
		 VerticalAlignment="Top" Width="120"
		 TextChanged="TextBox_TextChanged"/>
	<Button Content="Send" HorizontalAlignment="Left" Margin="135,10,0,0"
		VerticalAlignment="Top" Width="75"
		IsEnabled="False"
		Name="SendButton"/>
    </Grid>
</Window>

Example code: C#

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication12
{
    public partial class MainWindow : Window
    {
	public MainWindow()
	{
	    InitializeComponent();
	}

	private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
	{
	    // Cast TextBox.
	    TextBox box = sender as TextBox;

	    // If text is present, enable the button.
	    // ... Otherwise disable the button.
	    this.SendButton.IsEnabled = box.Text.Length > 0;
	}
    }
}

In the TextChanged event handler, we cast the sender object to a TextBox reference with the as-cast. Then, we access the Button by its name (SendButton). Its IsEnabled property is assigned to the result of an expression.

As

And: The expression evaluates to true when the TextBox has one or more characters, and false otherwise.

So: The Button becomes enabled when the TextBox has text. It remains disabled whenever the TextBox is empty.

Expressions can be used to make WPF programs better. Please notice how we use an expression to control the value of the IsEnabled property in the example. This means an if-statement is not required.

Instead: The IsEnabled property is tied to an expression. It handles true and false values correctly.

Property

And: The enabled state of the Button will not lose sync with the TextBox. This approach leads to UIs that are reliable.

When creating WPF (or Windows Forms) programs, logic rapidly becomes complex. The simple programs on this website misrepresent reality. Using expressions to maintain state, as with IsEnabled here, keeps things simpler.

Summary. In WPF programs, many concepts are used together. For example, a TextChanged event on a TextBox affects the IsEnabled property of a Button. And a bool property can be set with a C# expression.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf