Skip to content

Commit

Permalink
Stepper ValueChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlacey committed Feb 22, 2025
1 parent ab8cd09 commit ad79090
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static void OnValueChangedChanged(BindableObject bindable, object oldVal
{
for (int i = sldr.Behaviors.Count; i >= 0; i--)
{
if (sldr.Behaviors[i] is EventToCommandValueChangedBehavior oldBehavior && oldBehavior.CommandToInvoke == oldCmd)
if (sldr.Behaviors[i] is EventToCommandSliderValueChangedBehavior oldBehavior && oldBehavior.CommandToInvoke == oldCmd)
{
sldr.Behaviors.Remove(oldBehavior);
break;
Expand All @@ -31,12 +31,32 @@ private static void OnValueChangedChanged(BindableObject bindable, object oldVal

if (newValue is ICommand newCmd)
{
sldr.Behaviors.Add(new EventToCommandValueChangedBehavior { CommandToInvoke = newCmd });
sldr.Behaviors.Add(new EventToCommandSliderValueChangedBehavior { CommandToInvoke = newCmd });
}
}
else
if (bindable is Stepper stpr)
{
if (oldValue is ICommand oldCmd)
{
for (int i = stpr.Behaviors.Count; i >= 0; i--)
{
if (stpr.Behaviors[i] is EventToCommandStepperValueChangedBehavior oldBehavior && oldBehavior.CommandToInvoke == oldCmd)
{
stpr.Behaviors.Remove(oldBehavior);
break;
}
}
}

if (newValue is ICommand newCmd)
{
stpr.Behaviors.Add(new EventToCommandStepperValueChangedBehavior { CommandToInvoke = newCmd });
}
}
}

internal partial class EventToCommandValueChangedBehavior : Behavior<Slider>
internal partial class EventToCommandSliderValueChangedBehavior : Behavior<Slider>
{
public ICommand? CommandToInvoke { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Windows.Input;

namespace RapidXaml;

public partial class EventToCommand : BindableObject
{
internal partial class EventToCommandStepperValueChangedBehavior : Behavior<Stepper>
{
public ICommand? CommandToInvoke { get; set; }

protected override void OnAttachedTo(Stepper stpr)
{
stpr.ValueChanged += OnSliderValueChanged;
base.OnAttachedTo(stpr);
}

protected override void OnDetachingFrom(Stepper stpr)
{
stpr.ValueChanged -= OnSliderValueChanged;
base.OnDetachingFrom(stpr);
}

private void OnSliderValueChanged(object? sender, EventArgs e)
{
if (this.CommandToInvoke != null)
{
if (this.CommandToInvoke.CanExecute(null))
{
this.CommandToInvoke.Execute(null);
}
}
}
}
}

0 comments on commit ad79090

Please sign in to comment.