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 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

This comment has been minimized.

Copy link
Collaborator

@PedroLamas PedroLamas commented Feb 22, 2017

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

This comment has been minimized.

Copy link
Author

@RaminMT RaminMT commented Feb 22, 2017

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

@PedroLamas

This comment has been minimized.

Copy link
Collaborator

@PedroLamas PedroLamas commented Feb 22, 2017

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

@RaminMT RaminMT closed this Feb 22, 2017
@HppZ

This comment has been minimized.

Copy link

@HppZ HppZ commented Jul 17, 2017

what if I need sender and args?

@PedroLamas

This comment has been minimized.

Copy link
Collaborator

@PedroLamas PedroLamas commented Jul 17, 2017

@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

This comment has been minimized.

Copy link

@HppZ HppZ commented Jul 18, 2017

OK, got you, thanks.

@HppZ

This comment has been minimized.

Copy link

@HppZ HppZ commented Jul 18, 2017

maybe you can add this feature.

@manupstairs

This comment has been minimized.

Copy link

@manupstairs 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

This comment has been minimized.

Copy link

@HppZ HppZ commented Mar 20, 2018

@manupstairs Binding to Source property of WebView using ElementName

@manupstairs

This comment has been minimized.

Copy link

@manupstairs 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

This comment has been minimized.

Copy link
Collaborator

@PedroLamas PedroLamas commented Mar 21, 2018

@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

This comment has been minimized.

Copy link

@manupstairs manupstairs commented Mar 26, 2018

Very Cool! Thanks Pedrolamas!

@bromoapp

This comment has been minimized.

Copy link

@bromoapp 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

This comment has been minimized.

Copy link
Collaborator

@PedroLamas PedroLamas commented Feb 13, 2019

@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

This comment has been minimized.

Copy link

@weitzhandler weitzhandler commented Jul 12, 2019

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

This comment has been minimized.

Copy link
Collaborator

@PedroLamas PedroLamas commented Jul 13, 2019

@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

This comment has been minimized.

Copy link

@weitzhandler weitzhandler commented Jul 13, 2019

@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
Projects
None yet
6 participants
You can’t perform that action at this time.