migration
Some checks failed
Build and publish package / Build (push) Failing after 16s
Build and publish package / Determine version (push) Has been skipped
Build and publish package / Pack (push) Has been skipped
Build and publish package / Publish (push) Has been skipped

This commit is contained in:
2026-02-15 17:47:49 +01:00
Unverified
parent 5175a3b4b3
commit 9e194fa643
11 changed files with 877 additions and 1 deletions

View File

@@ -0,0 +1,91 @@
using Microsoft.UI.Xaml;
using Microsoft.Xaml.Interactivity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace SimpleToolkit.UI.WinUI.Behaviors
{
public class EventToCommandBehavior : Behavior<FrameworkElement>
{
#region FIELDS
private Delegate _handler;
private EventInfo _oldEvent;
#endregion
#region PROPERTIES
public string Event { get { return (string)GetValue(EventProperty); } set { SetValue(EventProperty, value); } }
public static readonly DependencyProperty EventProperty = DependencyProperty.Register("Event", typeof(string), typeof(EventToCommandBehavior), new PropertyMetadata(null, OnEventChanged));
public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } }
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(EventToCommandBehavior), new PropertyMetadata(null));
public bool PassArguments { get { return (bool)GetValue(PassArgumentsProperty); } set { SetValue(PassArgumentsProperty, value); } }
public static readonly DependencyProperty PassArgumentsProperty = DependencyProperty.Register("PassArguments", typeof(bool), typeof(EventToCommandBehavior), new PropertyMetadata(false));
#endregion
#region PRIVATE METHODS
private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
EventToCommandBehavior beh = (EventToCommandBehavior)d;
if (beh.AssociatedObject != null)
{
beh.AttachHandler((string)e.NewValue);
}
}
protected override void OnAttached()
{
AttachHandler(this.Event);
}
private void AttachHandler(string eventName)
{
if (_oldEvent != null)
{
_oldEvent.RemoveEventHandler(this.AssociatedObject, _handler);
}
if (!string.IsNullOrEmpty(eventName))
{
EventInfo ei = this.AssociatedObject.GetType().GetEvent(eventName);
if (ei != null)
{
MethodInfo mi = this.GetType().GetMethod("ExecuteCommand", BindingFlags.Instance | BindingFlags.NonPublic);
_handler = Delegate.CreateDelegate(ei.EventHandlerType, this, mi);
ei.AddEventHandler(this.AssociatedObject, _handler);
_oldEvent = ei;
}
else
{
throw new ArgumentException(string.Format("The event '{0}' was not found on type '{1}'", eventName, this.AssociatedObject.GetType().Name));
}
}
}
private void ExecuteCommand(object sender, object e)
{
object parameter = this.PassArguments ? e : null;
if (this.Command != null && this.Command.CanExecute(parameter))
{
this.Command.Execute(parameter);
}
}
#endregion
}
}

View File

@@ -0,0 +1,56 @@
# SimpleToolkit.UI.WinUI.Behaviors
### Set of helpers, class extensions, UI controls used in my other C# projects
SimpleToolkit is package of useful classes, helpers, extensions and UI controls, I use in my C# projects. UI.WinUI.Behaviors subpackage contains XAML action triggers for WinUI applications.
---
## NuGet registry status
| Subpackage | Status |
|--------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **SimpleToolkit.UI.WinUI.Behaviors** | [![NuGet version (SimpleToolkit.UI.WinUI.Behaviors)](https://img.shields.io/nuget/v/SimpleToolkit.UI.WinUI.Behaviors.svg?style=flat-square)](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Behaviors/) |
| SimpleToolkit.Extensions | [![NuGet version (SimpleToolkit.Extensions)](https://img.shields.io/nuget/v/SimpleToolkit.Extensions.svg?style=flat-square)](https://www.nuget.org/packages/SimpleToolkit.Extensions/) |
| SimpleToolkit.Attributes | [![NuGet version (SimpleToolkit.Attributes)](https://img.shields.io/nuget/v/SimpleToolkit.Attributes.svg?style=flat-square)](https://www.nuget.org/packages/SimpleToolkit.Attributes/) |
| SimpleToolkit.MVVM | [![NuGet version (SimpleToolkit.MVVM)](https://img.shields.io/nuget/v/SimpleToolkit.MVVM.svg?style=flat-square)](https://www.nuget.org/packages/SimpleToolkit.MVVM/) |
| SimpleToolkit.UI.Models | [![NuGet version (SimpleToolkit.UI.Models)](https://img.shields.io/nuget/v/SimpleToolkit.UI.Models.svg?style=flat-square)](https://www.nuget.org/packages/SimpleToolkit.UI.Models/) |
| SimpleToolkit.UI.WinUI.Converters | [![NuGet version (SimpleToolkit.UI.WinUI.Converters)](https://img.shields.io/nuget/v/SimpleToolkit.UI.WinUI.Converters.svg?style=flat-square)](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Converters/) |
| SimpleToolkit.UI.WinUI.Controls | [![NuGet version (SimpleToolkit.UI.WinUI.Controls)](https://img.shields.io/nuget/v/SimpleToolkit.UI.WinUI.Controls.svg?style=flat-square)](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Controls/) |
| SimpleToolkit.UI.WinUI.Helpers | [![NuGet version (SimpleToolkit.UI.WinUI.Helpers)](https://img.shields.io/nuget/v/SimpleToolkit.UI.WinUI.Helpers.svg?style=flat-square)](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Helpers/) |
## Features
- **EventToCommandBehavior** - allows to bind parametrized command to any event
## Dependencies
Dependencies should be installed automatically with this package
- Microsoft.Xaml.Behaviors.WinUI.Managed 2.0.9
## Installation and usage
You can download package from official NuGet registry or .nupkg file itself from Releases tab.
**CLI:**
```
dotnet add package SimpleToolkit.UI.WinUI.Behaviors
```
**Package reference in .csproj file:**
```
<PackageReference Include="SimpleToolkit.UI.WinUI.Behaviors" Version="<version>" />
```
## Attribution and contribution
This project is open source on MIT License, so you can just copy and upload again to your repository. But according to the license, you must include information about the original author. You can find license [here](https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.UI.WinUI.Behaviors/src/branch/main/LICENSE).
However, the preferred way to contribute would be to propose improvements in a pull request, through issues, or through other means of communication.
**Other sources:**
- Icon by [Icons8](icons8.com)

View File

@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>SimpleToolkit.UI.WinUI.Behaviors</RootNamespace>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<UseWinUI>true</UseWinUI>
<UseRidGraph>true</UseRidGraph>
<PackageReadmeFile>README.md</PackageReadmeFile>
<EnableCoreMrtTooling Condition=" '$(BuildingInsideVisualStudio)' != 'true' ">false</EnableCoreMrtTooling>
<Nullable>enable</Nullable>
<Title>SimpleToolkit.UI.WinUI.Behaviors</Title>
<Authors>Mateusz Skoczek</Authors>
<Copyright>Mateusz Skoczek</Copyright>
<PackageProjectUrl>https://repos.mateuszskoczek.com/SimpleToolkit/</PackageProjectUrl>
<PackageLicenseUrl>https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.UI.WinUI.Behaviors/src/branch/main/LICENSE</PackageLicenseUrl>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.UI.WinUI.Behaviors</RepositoryUrl>
<Description>Set of helpers, class extensions, UI controls used in my other C# projects - XAML action triggers for WinUI applications</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
</ItemGroup>
<ItemGroup>
<None Update="icon.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Update="README.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB