1

I have read articles about how commanding works different inside of a listview so I tried that code but when I click nothing happens. I am using Template10. Most of the example I find are for WPF which has incompatible pieces. Just need the bare minimum to get the button click to call the method below. The relevant parts of my code are :

  <ListView x:Name="lvMain"
                      ItemsSource="{Binding LeadSpeakerItems}"
                      SelectedItem="{Binding Lsi}">
                <ListView.ItemTemplate>

...

       <Button Content="Details"
                                    Command="{Binding ElementName=Root, Path=RunCommand}"
                                    Grid.Column="1" />
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>

And the code:

        public ICommand RunCommand { get; private set; }
    public MainPageViewModel()
    {
        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            LeadSpeakerItems.Add(new LeadSpeakerItem {
                VelocifyLeadTitle = "The is the lead title that says somrthing about something and her a number 234-456-3454",
                VelocifyFirstName = "BobbiMinajobi",
                VelocifyLastName = "Luciferdissikusliskus",
                VelocifyLoanAmount = 254000.00,
                VelocifyHomeValue = 278000.00
            });
        }

        RunCommand = new DelegateCommand<object>(OnRunCommand, CanRunCommand);
    }

      private void OnRunCommand(object obj)
    {
        // use the SelectedCustomer object here...
    }

    private bool CanRunCommand(object obj)
    {
        return true;
    } 

EDIT 1:

How would I get that particular item when the button or the listview item is selected? I am trying to get this piece of code run when that happens. I am missing something.

      set
        {
            Set(ref selectedItem, value);
        }

3 Answers 3

3

Supposing Root is your page or another control with your viewmodel as DataContext, you should alter your XAML to:

<Button Content="Details"
        Command="{Binding ElementName=Root, Path=DataContext.RunCommand}"
        Grid.Column="1" />

as RunCommand itself is not known to your Root object, but DataContext (your vm) is.

Sign up to request clarification or add additional context in comments.

1 Comment

it works! took me hours just for this simple change. Makes it more confusing that some answers are for WPF or other version that are not compatible with UWP when I searched for the answer on my own
1
<Button Content="Details"
                        Command="{Binding RunCommand}"
                        Grid.Column="1" />

or

<ListView 
     x:Name="lvMain"
     DataContext={Binding}>
     ....
</ListView>

<Button 
    DataContext="{Binding ElementName=lvMain, Path=DataContext}"
    Content="Details"
    Command="{Binding RunCommand}"
    Grid.Column="1" />

Comments

1

try use Template10.Mvvm.DelegateCommand for example

in viewmodel

public ICommand ItemSelected
        {
            get
            {
                return new Template10.Mvvm.DelegateCommand<string>((s) =>
                 {
                     NavigationService.Navigate(typeof(DetailPage), s);

                 });
            }

        }

add to your page

<page
xmlns:Behaviors="using:Template10.Behaviors"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:vm="using:....ViewModel"
....>
<Page.DataContext>
    <vm:ViewModel />
</Page.DataContext>

in your listview

    <ListView x:Name="listView" ... ItemsSource="{x:Bind ViewModel.ListItem}" >
                   <Interactivity:Interaction.Behaviors>
                    <Core:EventTriggerBehavior EventName="Tapped">
                        <Core:InvokeCommandAction     Command="{x:Bind ViewModel.ItemSelected}"     CommandParameter="{Binding ElementName=listView,Path=SelectedItem}"/>
                    </Core:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
</ListView>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.