IsEnabled Property (Button Example)
This page was last reviewed on Mar 14, 2023.
Dot Net Perls
IsEnabled. This property 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.
Getting started. First, we can add a TextBox and a Button to the Window, which nests the controls within a Grid. On the Button, please set IsEnabled to false.
Attributes example. On the TextBox I added a TextChanged event handler. Press tab after typing TextChanged in the XAML. Then we add code to the TextBox_TextChanged event handler.
Start On the Button, I added a Name attribute. This makes the Button easy to access in C# code by the identifier "SendButton."
Detail Here we cast the sender object to a TextBox reference with the as-cast. Then, we access the Button by its name (SendButton).
And IsEnabled is assigned to the result of an expression. The expression evaluates to true when the TextBox has one or more characters.
So The Button becomes enabled when the TextBox has text. It remains disabled whenever the TextBox is empty.
<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>
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; } } }
Expressions. 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.
Detail The IsEnabled property is tied to an expression. It handles true and false values correctly.
And The enabled state of the Button will not lose sync with the TextBox. This approach leads to UIs that are reliable.
A summary. In WPF programs, many concepts are used together. For example, a TextChanged event on a TextBox affects the IsEnabled property of a Button.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 14, 2023 (edit).
© 2007-2024 Sam Allen.