Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:02:04+00:00 2026-06-18T09:02:04+00:00

I am currently doing a test with ICommand and I wonder why ICommand does

  • 0

I am currently doing a test with ICommand and I wonder why ICommand does not fire with button in ListBox.ItemTemplate. But when used outside the template, it works.

here’s the window xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1" x:Class="WpfApplication1.Window2"
        Title="Window2" Height="300" Width="300">
    <Window.DataContext>
        <local:W2VM/>
    </Window.DataContext>

    <Grid>
        <ListBox x:Name="listHistory" BorderThickness="0" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" ItemsSource="{Binding History}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding ''}" />
                        <Button 
                            Grid.Column="1" 
                            HorizontalAlignment="Right" 
                            x:Uid="btnDeleteHistoryItem" 
                            x:Name="btnDeleteHistoryItem" 
                            Content="r" 
                            FontFamily="Marlett" 
                            Visibility="Hidden" Command="{Binding MeClick}" 

                            />
                    </Grid>

                    <DataTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Visibility" TargetName="btnDeleteHistoryItem" Value="Visible" />
                        </Trigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <Button Content=" exit " VerticalAlignment="Bottom" Command="{Binding ExitCommand}" />
    </Grid>
</Window>

here’s the complete ViewModel code

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }
    }

    public class W2VM : ViewModelBase
    {
        public List<string> _History = new List<string>();
        public List<string> History
        {
            get { return this._History; }
        }

        public ICommand MeClick
        {
            get;
            internal set;
        }

        public ICommand ExitCommand
        {
            get;
            internal set;
        }

        public W2VM()
        {
            this.History.AddRange(new string[] {
                "jayson", "hello", "world"
            });

            this.MeClick = new RelayCommand(Test);
            this.ExitCommand = new RelayCommand(Exit);
        }

        void Exit()
        {
            Application.Current.Shutdown();
        }

        void Test()
        {
            Debug.WriteLine("hello world");

            MessageBox.Show("do something incredible");
        }
    }
}

Test() does not fire


ok I got it working..

<Window.Resources>
    <me:W2VM x:Key="local" />
</Window.Resources>

instead of

<Window.DataContext>
    <local:W2VM/>
</Window.DataContext>

and my window grid

<Grid DataContext="{StaticResource local}">
    <ListBox x:Name="listHistory" BorderThickness="0" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" ItemsSource="{Binding History}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding ''}" />
                    <Button 
                        Grid.Column="1" 
                        HorizontalAlignment="Right" 
                        x:Uid="btnDeleteHistoryItem" 
                        x:Name="btnDeleteHistoryItem" 
                        Content="r" 
                        FontFamily="Marlett" 
                        Visibility="Hidden" Command="{Binding MeClick, Source={StaticResource local}}"  />
                </Grid>

                <DataTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Visibility" TargetName="btnDeleteHistoryItem" Value="Visible" />
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <Button Content=" exit " VerticalAlignment="Bottom" Command="{Binding ExitCommand}" />
</Grid>

now this will raise another question.

How would I know which ListViewItem was clicked when the btnDeleteHistoryItem command was just routed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:02:06+00:00Added an answer on June 18, 2026 at 9:02 am

    Are you using the relaycommand of mvvm-light?
    I use command parameters to pass information to the command method:

    <Button 
                            Grid.Column="1" 
                            HorizontalAlignment="Right" 
                            x:Uid="btnDeleteHistoryItem" 
                            x:Name="btnDeleteHistoryItem" 
                            Content="r" 
                            FontFamily="Marlett" 
                            Visibility="Hidden" Command="{Binding MeClick, Source=StaticResource local}}" 
                            CommandParameter="YourUniqueIdentifyingVariable"  />
    

    and then in your view model:

    public RelayCommand<string> MeClick{
        get { return _MyClick; }
        private set { _MyClick = value; }
    }
    
    public New()
    {
    
        MeClick = new RelayCommand<string>(MySub, MySubIsEnabled);
    }
    
    private void MySub(string sParam)
    {
        //do stuff here, YourUniqueIdentifyingVariable is sParam
    }
    
    private bool MySubIsEnabled(string sParam)
    {
        return true;
    }
    

    you can bind “YourUniqueIdentifyingVariable” to the ID of the record or some other value that you can process in your command method

    Code is untested but should work

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently doing JUnit test for my hospital administration System. But When Attempt
I'm currently doing the following to compensate for boolean's not mapping well to radio
I am currently doing a C# WPF application that generates a table that does
I am currently doing unit testing, and use folders to organize my test cases.
Currently doing a test plan for a system. May I know what are the
I'm currently doing some test where I try to delete a site collection programmatically.
I am currently doing an online test system, I want allow admin to create
Currently doing a test-app with JCR (Modeshape). The abstracted flow is as follows: session.open,
I'm currently doing a test with a LiveWallpaper in Android. I am drawing something
I'm currently doing some testing on wordpress. In a recent test I found out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.