Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory.EventValueCommand does not handle null properly..(?) #9

Closed
ruxo opened this issue Dec 8, 2015 · 4 comments
Closed

Factory.EventValueCommand does not handle null properly..(?) #9

ruxo opened this issue Dec 8, 2015 · 4 comments

Comments

@ruxo
Copy link

ruxo commented Dec 8, 2015

I'm trying to use EventValueCommand method of EventViewModelBase to create a command for a button. My code look like this:

type MainWindowEvents = Invalid

type MainWindowModel() as me =
    inherit EventViewModelBase<MainWindowEvents>()

    let navCommand = me.Factory.EventValueCommand(fun x -> System.Diagnostics.Debug.Print (x.ToString()); Invalid)

    do me.EventStream |> Observable.subscribe (kprintf Debug.Print "get %A") |> ignore

    member x.NavCommand = navCommand

and in my XAML, I have a button that fires that command.

     <Button Command="{Binding NavCommand}" CommandParameter="hello guys!">About sample</Button>

When the button clicks, it does not work. I've debug and found that navCommand.CanExecute return false and the root cause is null value is passed into the handle function.

I'm not sure if I use the function correctly as it is by designed, or is this supposed to be a defect?

@ReedCopsey
Copy link
Member

@ruxo The EventValueCommand is meant to be used where the CommandParameter is of the generic type of the Event type. In this case, it'd mean you'd need a converter to pass the parameter of type MainWindowEvents.

What are you trying to do? There are a couple of approaches -

  1. You could use the normal command infrastructure from ViewModelBase to just create a command that accepts a string parameter, then optionally add it to your event stream if you need at that point.
  2. Write a converter to convert from your actual parameter type to the event type. This is what the sample applications in FsXaml all do.

@ruxo
Copy link
Author

ruxo commented Dec 9, 2015

I'm interested in Event Stream and trying to do MVC with it (inspired by a blog post from http://marisks.net/). In my real XAML, there is also a TreeView which I also uses for selecting item. The TreeViewItem uses EventToCommand to convert Selected event to MainWindowEvents. Like following snippet:

      <TreeViewItem Header="About sample" Tag="AboutDialog.xaml">
        <i:Interaction.Triggers><i:EventTrigger EventName="Selected"><fsx:EventToCommand Command="{Binding EventCommand}" EventArgsConverter="{StaticResource selectConverter}" /> </i:EventTrigger> </i:Interaction.Triggers>
      </TreeViewItem>

Now I'm trying to use buttons instead of the TreeView. I guess I can use EventTrigger to do the same thing but I'm curious if there is a shorter way to do so.

So I have to apply a converter. Do I have to implement it myself (something like EventArgsConverter) or is there already a helper in the library? Sorry I'm new to this library. Thank you for your help :)

@ReedCopsey
Copy link
Member

You'd need to make your own converter. If you inherit inherit EventViewModelBase<string>() instead of MainWindowEvents, using a string as the command parameter will work, though.

@ruxo
Copy link
Author

ruxo commented Dec 17, 2015

Just post here for reference: http://stackoverflow.com/questions/335849/wpf-commandparameter-is-null-first-time-canexecute-is-called

So the true culprit is setting CommandParameter does not re-evaluate CanExecute method. Simple workaround is just swap the order of Command and CommandParameter in the XAML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants