Enable or Disable Button using CheckBox in Silverlight

Published on Monday, August 22, 2011

How did you use a checkbox’s checked status to enable or disable a button? Normally, in real project, we do not enable or disable a single button but a group of buttons or controls, here we just use a single button to descript our case.

There is a feature named “Element to Element Binding” which we can use conveniently, we do not need to write any code behind.

slbind003
<UserControl x:Class="SLDataBind2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Border BorderThickness="1" Width="150">
            <StackPanel>
                <CheckBox IsChecked="True" Content="Enable Button" Margin="2" x:Name="chkEnableButton" />
                <ToggleButton Margin="2" Content="Toggle Button" x:Name="tbtToggle" IsEnabled="{Binding IsChecked, Mode=OneWay, ElementName=chkEnableButton}" />

            </StackPanel>
        </Border>
    </Grid>
</UserControl>