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
988 stars 143 forks source link

Menus #163

Closed chasihle closed 3 years ago

chasihle commented 3 years ago

Hi, Do you have examples for implementing menus? I can't bind a menu item to a ViewModel method. In Caliburn.Micro I just had to name the menu item the same as the method in ShellView but doesn't work in Stylet. Any help you can provide would be greatly appreciated.

Thanks, Charlie

canton7 commented 3 years ago

They work the same as anything else with a Command property:

<MenuItem Command="{s:Action SomeMethodOnTheViewModel}"/>

Can you please post the code you're having trouble with?

chasihle commented 3 years ago

Attached are the View and ShellViewModel files. When I run It now I get the following error

Stylet.Xaml.ActionNotSetException: 'View.ActionTarget not set on control System.Windows.Controls.MenuItem Header:Create Customer Items.Count:0 (method CreatCust()). This probably means the control hasn't inherited it from a parent, e.g. because a ContextMenu or Popup sits in the visual tree. You will need so set 's:View.ActionTarget' explicitly. See the wiki section "Actions" for more details.'

NewZip.zip

canton7 commented 3 years ago

I hacked that code into a project which I could actually run, SmallBusCustMgtWPF.zip, and the menu works fine.

Please provide a minimal reproducible sample which I can run and which reproduces your issue. Thanks!

chasihle commented 3 years ago

Hey, Still doesn't work for me. I get the same error I included in my previous email. I've attached a zip of the entire solution and the local SQL DB. This isso simple and it's kicking my butt [Uploading StyletSmallBusinessCustMgmnt.zip…]() :)

canton7 commented 3 years ago

So clicking "new customer" in the zip I uploaded fails for you?

canton7 commented 3 years ago

Can you try and create a minimal repro? https://stackoverflow.com/help/minimal-reproducible-example

canton7 commented 3 years ago

(Also, that zip you linked is just a link to this issue -- there's no zip there I'm afraid)

chasihle commented 3 years ago

Hey Antony, I was going through my solution this morning commenting out anything related to DB access, removing some class files related to things non UI and of course it still failed. So before I zipped it up to send to you I took your project and added in the DB access and other class files and everything works like a charm. Go figure. I don’t know what the differences are, I can’t see any. It seems that under .NetCore something must get corrupted and by creating a new project and adding in the same objects it will work.

Anyway, I don’t know if you want me to still send you my original solution, I will if you want .

Thanks very much for your help

Charlie

Sent from C. Ihle's iPhone 704-439-6270

On Oct 22, 2020, at 4:20 PM, Antony Male notifications@github.com wrote:

 (Also, that zip you linked is just a link to that issue -- there's no zip there I'm afraid)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

canton7 commented 3 years ago

One thing I did change was I removed the assignment of the DataContext from the code behind: Stylet takes care of instantiating and assigning the ViewModel. Can youssee whether that was the cause?

I'm not sure how it could have been, as that would have been overridden by Stylet straight away, but it's worth checking.

chasihle commented 3 years ago

Antony, I still get the error in my original project even after removing DataContext from the code behind

Charlie

Sent from C. Ihle's iPhone 704-439-6270

On Oct 22, 2020, at 6:29 PM, Antony Male notifications@github.com wrote:

 One thing I did change was I removed the assignment of the DataContext from the code behind: Stylet takes care of instantiating and assigning the ViewModel. Can youssee whether that was the cause?

I'm not sure how it could have been, as that would have been overridden by Stylet straight away, but it's worth checking.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

canton7 commented 3 years ago

The other possibility is that the project I put together targets netcoreapp: does yours target framework?

chasihle commented 3 years ago

Yes, the project is netcore 3.1. I’ve noticed that with my Core projects as I try different things, add/remove packages etc.. it’s almost like something gets corrupted. I’ll create a new project add in the existing items from the problem project and it will work. That’s what I just did with the project you hacked together and it’s been working fine.

Sent from C. Ihle's iPhone 704-439-6270

On Oct 23, 2020, at 1:32 PM, Antony Male notifications@github.com wrote:

 The other possibility is that the project I put together targets netcoreapp: does yours target framework?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

canton7 commented 3 years ago

Closing as I don't think there's anything else to do here. Please re-open if necessary. Thanks!