ToggleButton check/uncheck event : ToggleButton « Windows Presentation Foundation « C# / CSharp Tutorial






using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

    public class ToggleTheButton : Window
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new ToggleTheButton());
        }
        public ToggleTheButton()
        {
            ToggleButton btn = new ToggleButton();
            btn.Content = "click";
            btn.HorizontalAlignment = HorizontalAlignment.Center;
            btn.VerticalAlignment = VerticalAlignment.Center;
            btn.IsChecked = (ResizeMode == ResizeMode.CanResize);
            btn.Checked += ButtonOnChecked;
            btn.Unchecked += ButtonOnChecked;
            Content = btn;
        }
        void ButtonOnChecked(object sender, RoutedEventArgs args)
        {
            ToggleButton btn = sender as ToggleButton;
            ResizeMode = (bool)btn.IsChecked ? ResizeMode.CanResize : ResizeMode.NoResize;
        }
    }








24.6.ToggleButton
24.6.1.Create ToggleButton and set string as content, set HorizontalAlignment and VerticalAlignment for ToggleButton
24.6.2.Set data binding for ToggleButton, Set DataContext for ToggleButton
24.6.3.Add tooltop for ToggleButton
24.6.4.ToggleButton check/uncheck event