microsoft / XamlBehaviors

This is the official home for UWP XAML Behaviors on GitHub.
MIT License
697 stars 112 forks source link

EventArgs as CommandParameter on InvokeCommandAction #126

Closed RaminMT closed 7 years ago

RaminMT commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

pedrolamas commented 7 years ago

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

HppZ commented 7 years ago

what if I need sender and args?

pedrolamas commented 7 years ago

@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 commented 7 years ago

OK, got you, thanks.

HppZ commented 7 years ago

maybe you can add this feature.

manupstairs commented 6 years ago

Hi @PedroLamas , my question is: Is it possible to binding one property of xxxEventArgs with CommandParameter? For example, I want pass Uriof 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 commented 6 years ago

@manupstairs Binding to Source property of WebView using ElementName

manupstairs commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

Very Cool! Thanks Pedrolamas!

bromoapp commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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