Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/helpers/command-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Provides Command/CommandParameter attached properties for common scenarios.
- `NavigationView.ItemInvoked`
- `ItemsRepeater` item tapped
- `TextBox` and `PasswordBox` when the Enter key is pressed
- `ToggleSwitch` toggled
- any `UIElement` tapped
- For `Command`, the relevant parameter is also provided for the `CanExecute` and `Execute` call:
> `CommandParameter` can be set, on the item-container or item-template's root for collection-type control, or control itself for other controls, to replace the following.
Expand All @@ -29,6 +30,7 @@ Provides Command/CommandParameter attached properties for common scenarios.
- `NavigationViewItemInvokedEventArgs.InvokedItem` from `NavigationView.ItemInvoked`
- `ItemsRepeater`'s item root's DataContext
- `TextBox.Text`
- `ToggleSwitch.IsOn`
- `PasswordBox.Password`
- `UIElement` itself
- `Command` on `TextBox`/`PasswordBox`\*: Having this set will also cause the keyboard to dismiss on enter.
Expand All @@ -47,6 +49,9 @@ xmlns:utu="using:Uno.Toolkit.UI"
<!-- Execute command on enter -->
<PasswordBox utu:CommandExtensions.Command="{Binding Login}" />

<!-- Execute command on toggle -->
<ToggleSwitch utu:CommandExtensions.Command="{Binding SetDarkMode}" />

<!-- ListView item click-->
<ListView ItemsSource="123"
IsItemClickEnabled="True"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
<TextBox utu:CommandExtensions.Command="{Binding DebugInputCommand}" />
</StackPanel>

<!-- ToggleSwitch[Command] example -->
<StackPanel Spacing="8">
<TextBlock Text="- ToggleSwitch toggled:" />
<TextBlock Text="{Binding ToggleSwitchText}" />
<ToggleSwitch utu:CommandExtensions.Command="{Binding DebugToggleSwitchCommand}" />
</StackPanel>

<!-- ListView[Command] example -->
<StackPanel Spacing="8">
<TextBlock Text="- ListView item click:" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ public class CommandExtensionsSamplePageVM : ViewModelBase
public string[] Fruits { get; } = new[] { "Apple", "Banana", "Cactus" };

public string InputDebugText { get => GetProperty<string>(); set => SetProperty(value); }
public string ToggleSwitchText { get => GetProperty<string>(); set => SetProperty(value); }
public string ListViewDebugText { get => GetProperty<string>(); set => SetProperty(value); }
public string SelectorDebugText { get => GetProperty<string>(); set => SetProperty(value); }
public string NavigationDebugText { get => GetProperty<string>(); set => SetProperty(value); }
public string ItemsRepeaterDebugText { get => GetProperty<string>(); set => SetProperty(value); }
public string ElementDebugText { get => GetProperty<string>(); set => SetProperty(value); }

public ICommand DebugInputCommand => new Command(DebugInput);
public ICommand DebugToggleSwitchCommand => new Command(DebugToggleSwitch);
public ICommand DebugListViewCommand => new Command(DebugListView);
public ICommand DebugSelectorCommand => new Command(DebugSelector);
public ICommand DebugNavigationCommand => new Command(DebugNavigation);
public ICommand DebugItemsRepeaterCommand => new Command(DebugItemsRepeater);
public ICommand DebugElementTappedCommand => new Command(DebugElement);

private void DebugInput(object parameter) => InputDebugText = Invariant($"{DateTime.Now:HH:mm:ss}: parameter={parameter}");
private void DebugToggleSwitch(object parameter) => ToggleSwitchText = Invariant($"{DateTime.Now:HH:mm:ss}: parameter={parameter}");
private void DebugListView(object parameter) => ListViewDebugText = Invariant($"{DateTime.Now:HH:mm:ss}: parameter={parameter}");
private void DebugSelector(object parameter) => SelectorDebugText = Invariant($"{DateTime.Now:HH:mm:ss}: parameter={parameter}");
private void DebugNavigation(object parameter) => NavigationDebugText = Invariant($"{DateTime.Now:HH:mm:ss}: parameter={parameter}");
Expand Down
7 changes: 0 additions & 7 deletions samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples/ReadMe.md

This file was deleted.

17 changes: 16 additions & 1 deletion src/Uno.Toolkit.UI/Behaviors/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class CommandExtensions

/// <summary>
/// Backing property for the command to execute when <see cref="TextBox"/>/<see cref="PasswordBox"/> enter key is pressed,
/// <see cref="ListViewBase.ItemClick" />, <see cref="Selector.SelectionChanged" />, <see cref="NavigationView.ItemInvoked" />, <see cref="ItemsRepeater" /> item tapped, or <see cref="UIElement.Tapped" />.
/// <see cref="ListViewBase.ItemClick" />, <see cref="Selector.SelectionChanged" />, <see cref="NavigationView.ItemInvoked" />, <see cref="ItemsRepeater" /> item tapped, <see cref="ToggleSwitch.Toggled"/> or <see cref="UIElement.Tapped" />.
/// </summary>
/// <remarks>
/// For Command, the relevant parameter is also provided for the <see cref="ICommand.CanExecute(object)"/> and <see cref="ICommand.Execute(object)"/> call:
Expand All @@ -44,6 +44,7 @@ public static class CommandExtensions
/// <item><see cref="Selector.SelectedItem"/></item>
/// <item><see cref="NavigationViewItemInvokedEventArgs.InvokedItem"/> from <see cref="NavigationView.ItemInvoked"/></item>
/// <item><see cref="ItemsRepeater"/>'s item root's DataContext</item>
/// <item><see cref="ToggleSwitch.IsOn"/></item>
/// <item><see cref="UIElement"/> itself</item>
/// </list>
/// <see cref="CommandParameterProperty"/> can be set, on the item-container or item-template's root for collection-type controls, or control itself for other controls, to replace the above.
Expand Down Expand Up @@ -120,6 +121,14 @@ private static void OnCommandChanged(DependencyObject sender, DependencyProperty
nv.ItemInvoked += OnNavigationViewItemInvoked;
}
}
else if (sender is ToggleSwitch ts)
{
ts.Toggled -= OnToggleSwitchToggled;
if (GetCommand(sender) is { } command)
{
ts.Toggled += OnToggleSwitchToggled;
}
}
else if (sender is UIElement uie)
{
uie.Tapped -= OnUIElementTapped;
Expand Down Expand Up @@ -198,6 +207,12 @@ private static void OnNavigationViewItemInvoked(NavigationView sender, Navigatio
{
TryInvokeCommand(sender, TryGetItemCommandParameter(e.InvokedItemContainer) ?? e.InvokedItem);
}
private static void OnToggleSwitchToggled(object sender, RoutedEventArgs e)
{
if (sender is not ToggleSwitch host) return;

TryInvokeCommand(host, host.IsOn);
}
private static void OnUIElementTapped(object sender, TappedRoutedEventArgs e)
{
if (sender is not UIElement host) return;
Expand Down
Loading