Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Command" property for ToggleSwitch control #1464

Closed
alex-buraykin opened this issue Jul 7, 2014 · 1 comment
Closed

Add "Command" property for ToggleSwitch control #1464

alex-buraykin opened this issue Jul 7, 2014 · 1 comment
Assignees
Milestone

Comments

@alex-buraykin
Copy link

Add "Command" property for ToggleSwitch control

@alex-buraykin
Copy link
Author

AttachedProperty:

using System;
using System.Windows;
using System.Windows.Input;
using MahApps.Metro.Controls;

namespace e3Shell.StandMaterialsSpecification.Behaviors
{
    public class ToggleSwitchBehavior
    {
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ToggleSwitchBehavior),
                new UIPropertyMetadata(null, OnIsExternalChanged));

        public static ICommand GetCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandProperty);
        }

        public static void SetCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }

        private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            var toggleSwitch = sender as ToggleSwitch;
            if (toggleSwitch == null) return;

            if (args.NewValue != null)
                toggleSwitch.IsCheckedChanged += IsCheckedChangedHandler;
            else
                toggleSwitch.IsCheckedChanged -= IsCheckedChangedHandler;
        }

        private static void IsCheckedChangedHandler(object sender, EventArgs e)
        {
            var command = GetCommand((ToggleSwitch)sender);
            if (command.CanExecute(null))
                command.Execute(null);
        }
    }
}

In your XAML:

xmlns:behaviors="clr-namespace:e3Shell.StandMaterialsSpecification.Behaviors"
<controls:ToggleSwitch behaviors:ToggleSwitchBehavior.Command="{Binding Path=OpenAboutFlyoutCommand}"/>

@punker76 punker76 added this to the 1.2.0 milestone May 30, 2015
@punker76 punker76 self-assigned this May 30, 2015
punker76 added a commit that referenced this issue May 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants