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

EventArgs as CommandParameter on InvokeCommandAction #126

Closed
RaminMT opened this issue Feb 22, 2017 · 17 comments
Closed

EventArgs as CommandParameter on InvokeCommandAction #126

RaminMT opened this issue Feb 22, 2017 · 17 comments

Comments

@RaminMT
Copy link

RaminMT commented Feb 22, 2017

Hi,
Is it possible to use event arguments as parameter for commands when using InvokeCommandAction?!
Actually I'm using ItemClick event with behaviors on a ListView & ItemClickEventArgs is the only way to know which item clicked.
Maybe It's good to notice for a reason I can't use SelectedItem, because It's null when you set SelectionMode="None" & also that I'm developing UWP app in C#.

Thanks

@pedrolamas
Copy link
Collaborator

By default, that's exactly what will be passed to the command!

Just make sure you don't set theCommandParameter or Converter properties and also that your command execution method can actually receive the parameter!

@RaminMT
Copy link
Author

RaminMT commented Feb 22, 2017

Oops!
Thank you! I didn't know that!

@pedrolamas
Copy link
Collaborator

No problem! Please do close this issue if this solves the problem! 😄

@RaminMT RaminMT closed this as completed Feb 22, 2017
@HppZ
Copy link

HppZ commented Jul 17, 2017

what if I need sender and args?

@pedrolamas
Copy link
Collaborator

@HppZ the sender is currently being ignored; if you need it, my advice is to copy the source code for the InvokeCommandAction to your project and change the Execute method to match your needs!

@HppZ
Copy link

HppZ commented Jul 18, 2017

OK, got you, thanks.

@HppZ
Copy link

HppZ commented Jul 18, 2017

maybe you can add this feature.

@manupstairs
Copy link

manupstairs commented Mar 20, 2018

Hi @pedrolamas , my question is:
Is it possible to binding one property of xxxEventArgs with CommandParameter?
For example, I want pass Uri of NavigationEventArgs. Because NavigationEventArgs belongs to View layer, we'd better not pass it to ViewModel.

<WebView local:WebViewEx.Uri="{Binding LoginUri, Mode=OneTime}">
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="LoadCompleted">
                <Core:InvokeCommandAction Command="{Binding LoginCommand}" CommandParameter="{Binding Uri}"/>
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
</WebView>

@HppZ
Copy link

HppZ commented Mar 20, 2018

@manupstairs Binding to Source property of WebView using ElementName

@manupstairs
Copy link

manupstairs commented Mar 20, 2018

Thanks @HppZ for you quickly reply. In fact I tried, it can works if I write
<Core:InvokeCommandAction Command="{Binding LoginCommand}" CommandParameter="{Binding ElementName=webView}"/>
I can find xxxWebView.Source is changed after LoadCompleted event.
But if I just write
<Core:InvokeCommandAction Command="{Binding LoginCommand}" CommandParameter="{Binding Source,ElementName=webView}"/>
The source of WebView will keep the origin value in ViewModel.
Sorry I forget to say I want to use WebView for a OAuth login. I wan to get response uri after user login.

@pedrolamas
Copy link
Collaborator

@manupstairs you can just create and pass a custom converter that will take the event args and return the uri from it!

Should look something like this:

<WebView local:WebViewEx.Uri="{Binding LoginUri, Mode=OneTime}">
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="LoadCompleted">
                <Core:InvokeCommandAction Command="{Binding LoginCommand}" InputConverter="{StaticResource MyCustomConverter}"/>
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
</WebView>

@manupstairs
Copy link

Very Cool! Thanks Pedrolamas!

@bromoapp
Copy link

bromoapp commented Feb 13, 2019

Hi, sorry to reopen this issue.
@pedrolamas or @manupstairs do you have any sample code for Custom Converter? I'm building an UWP app and using WebView too

Thanks

@pedrolamas
Copy link
Collaborator

@bromoapp all you need is a class that implements IValueConverter:

public class MyCustomConverter : Windows.UI.Xaml.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        //implement your conversion operation here

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        //you can leave this one like this as it's not required for this case

        throw new NotImplementedException();
    }
}

@weitzhandler
Copy link

I want to bind CommandParameter to the a property of the control that triggered the action, for example:

<NavigationView IsSettingsVisible="True">
  <i:Interaction.Behaviors>
    <core:EventTriggerBehavior EventName="ItemInvoked">
      <core:EventTriggerBehavior.Actions>
        <core:InvokeCommandAction Command="{Binding MenuNavigated}"
           CommandParameter="{Binding <InvokeItem>.Tag}" />
      </core:EventTriggerBehavior.Actions>
    </core:EventTriggerBehavior>
  </i:Interaction.Behaviors>
  <NavigationView.MenuItems>
    <NavigationViewItem Icon="Contact" Content="Contacts" Tag="contacts"/>
  </NavigationView.MenuItems>
</NavigationView>

How can I achieve that?

@pedrolamas
Copy link
Collaborator

@weitzhandler please don't reply to closed issues with new/different context of the issue itself. As what you wrote is more of a question than an issue, I also recomment that you use StackOverflow, tagging the question with the UWP tag.

Having said that, what you want should be possible by just adding a name to the control (like x:Name="ControlName") and then using {Binding TheControlProperty, ElementName=ControlName}

@weitzhandler
Copy link

@pedrolamas tx for your reply
There is no element name, it's auto generated from data-template. I want to refer to the currently selected NavigationViewItem.

Anyway, I switched from using a command to a SelectedItem in my VM and finished.

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

6 participants