Search This Blog

Wednesday, January 27, 2016

ToogleButton WPF Example

WPF > Controls  > ContentControl > ToogleButton

Toggle button is similar to CheckBox control. It holds its state when it clicked.

Example 
On/Off Button






<Window x:Class="WpfApplication3.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">
    <Window.Resources>
        <Style x:Key="ToggleButtonStyle" TargetType="ToggleButton">
            <Setter Property="Content" Value="Off"/>
            <Setter Property="Foreground" Value="Red" />
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="On">
                    </Setter>
                    <Setter Property="Foreground" Value="Green" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ToggleButton Name="btnToogle" IsChecked="True"
                    Height="30" Width="70"
                      Style="{StaticResource ToggleButtonStyle}">
        </ToggleButton>
    </Grid>
</Window>