Skip to content

Commit

Permalink
fix on/off label binding issue #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed May 30, 2015
1 parent ef8571b commit a8b00c1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
18 changes: 0 additions & 18 deletions MahApps.Metro/Controls/ToggleSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class ToggleSwitch : ContentControl
private const string SwitchPart = "Switch";

private ToggleButton _toggleButton;
private bool _wasContentSet;

public static readonly DependencyProperty OnLabelProperty = DependencyProperty.Register("OnLabel", typeof(string), typeof(ToggleSwitch), new PropertyMetadata("On"));
public static readonly DependencyProperty OffLabelProperty = DependencyProperty.Register("OffLabel", typeof(string), typeof(ToggleSwitch), new PropertyMetadata("Off"));
Expand Down Expand Up @@ -209,32 +208,15 @@ void ToggleSwitch_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs
IsChecked = !IsChecked;
}

private void SetDefaultContent()
{
Binding binding = new Binding("IsChecked") { Source = this, Converter = new OffOnConverter(), ConverterParameter = this };
SetBinding(ContentProperty, binding);
}

private void ChangeVisualState(bool useTransitions)
{
VisualStateManager.GoToState(this, IsEnabled ? NormalState : DisabledState, useTransitions);
}

protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
_wasContentSet = true;
}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

if (!_wasContentSet && GetBindingExpression(ContentProperty) == null)
{
SetDefaultContent();
}

if (_toggleButton != null)
{
_toggleButton.Checked -= CheckedHandler;
Expand Down
1 change: 1 addition & 0 deletions MahApps.Metro/Converters/OffOnConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace MahApps.Metro.Converters
{
[Obsolete(@"This converter will be deleted in the next release.")]
public class OffOnConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
Expand Down
22 changes: 22 additions & 0 deletions MahApps.Metro/Themes/ToggleSwitch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@
</Grid>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True" />
<Condition Property="Content" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter TargetName="Content" Property="Content">
<Setter.Value>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="OnLabel" Mode="TwoWay" />
</Setter.Value>
</Setter>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="False" />
<Condition Property="Content" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter TargetName="Content" Property="Content">
<Setter.Value>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="OffLabel" Mode="TwoWay" />
</Setter.Value>
</Setter>
</MultiTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Header}"
Value="{x:Null}">
<Setter TargetName="Header"
Expand Down

0 comments on commit a8b00c1

Please sign in to comment.