canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
990 stars 143 forks source link

Support binding Actions to Commands programmatically #124

Closed daawaan4U closed 4 years ago

daawaan4U commented 4 years ago

I have a ViewModel that has an ICommand property and would like to initialize the binding of that command property to a method using Actions in the ViewModel.

Something like:

public class Page : Screen, INavigationPage
{
    public ICommand Command => (ICommand)(new Action((PublishMyEvent)));
    . . .
    public void PublishMyEvent() => eventAggregator.Publish(new MyEvent());
}
canton7 commented 4 years ago

Why not use actions?

daawaan4U commented 4 years ago

I mean yeah, I'm referring to the Actions from this framework. It's just that the examples are done in xaml. Does the framework provide a way to do it in code?

daawaan4U commented 4 years ago

I'm binding the Command property above in a style for an ItemsControl inside a CustomControl.

 . . .
<Style TargetType="{x:Type ItemsControl}">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <local:Item Command="{Binding Command}" Icon="{Binding Icon}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>
. . . 

Since the {s:Action MethodName} doesn't work in Styles, I'm finding a way if Stylet's action can work in code-behind. Something similar to my first comment.

canton7 commented 4 years ago

Ah right. You'll have to write your own ICommand implementation. They're pretty straightforward.

On 24 April 2020 03:25:06 BST, DaaWaan notifications@github.com wrote:

I'm binding the Command property above in a style for an ItemsControl inside a CustomControl.

. . .

. . .

Since the {s:Action MethodName} doesn't work in Styles, I'm finding a way if Stylet's action can work in code-behind. Something similar to my first comment.

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/canton7/Stylet/issues/124#issuecomment-618766081