none
ListView.ItemTemplate with Button. Command doesn't execute when item is selected

    Question

  • I have a listview with an item template that contains a button:


    <ListView ItemsSource="{Binding AllProducts}">
          <ListView.ItemTemplate>
                <DataTemplate>
                      <Grid>
                            <Button Content="Add Supplier"
                                    Command="{Binding AddSupplier}"/>
                      </Grid>
                </DataTemplate>
          </ListView.ItemTemplate>
    </ListView>


    When the listitem is not selected and the button is clicked, the command executes fine. However, if the listitem is selected, the command does not execute. I have tried putting click and mouse down event handlers on the button, but the events do not fire.

    Is there any way to make the command execute when the listitem is selected?
    Friday, March 5, 2010 3:54 PM

All replies

  • You might want to try setting the CommandTarget. Here's some info from the Commanding topic on MSDN.

    The command target is the element on which the command is executed. With regards to a RoutedCommand, the command target is the element at which routing of the Executed and CanExecute starts. As noted previously, in WPF the CommandTarget property on ICommandSource is only applicable when the ICommand is a RoutedCommand. If the CommandTarget is set on an ICommandSource and the corresponding command is not a RoutedCommand, the command target is ignored.

    The command source can explicitly set the command target. If the command target is not defined, the element with keyboard focus will be used as the command target.

    Margaret

    Friday, March 5, 2010 6:35 PM
  • Thanks for the reply margaret.

    Unfortunately, setting the command target does not solve this problem. The command simply isn't being executed, it doesn't really make a difference which control the command is executed on.
    Tuesday, March 9, 2010 10:22 AM
  • I have the same problem than you...
    Friday, September 17, 2010 1:58 PM
  • Hi,

    Please check out this, if it helps you then use it, otherwise ignore it, because i dont understand what your problem is.

    <Grid>

            <ListView ItemsSource="{Binding Data}">

                <ListView.ItemTemplate>

                    <DataTemplate>

                        <StackPanel Orientation="Horizontal">

                            <TextBlock Text="{Binding}" />

                            <Button Content="Add Supplier"

                                    Command="{Binding Path=DataContext.AddSupplier,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}}" />

                        </StackPanel>

                    </DataTemplate>

                </ListView.ItemTemplate>

            </ListView>

        </Grid>

    in codebehind

    public Window3()

            {

                InitializeComponent();

                this.DataContext = this;

                GetItems();

                AddSupplier = new DelegateCommand<object>(OnSelectStationCommand);

            }

    private void OnSelectStationCommand(object sender)

            {

                //Person person = sender as Person;

                //FullNames.Add(person.username + ",");

                //StringBuilder names = new StringBuilder();

                //for (int i = 0; i < FullNames.Count; i++)

                //{

                //    names.Append(FullNames[i].ToString());

                //}

                //SelectedPerson.username = names.ToString();

                //NotifyPropertyChange("SelectedPerson");

            }

     

            public ICommand AddSupplier { get; set; }

            private void GetItems()

            {

                List<string> items = new List<string>

                    {

                        "Rajnikant","Bhavik","Hiren","Anil","Pragnesh","Chinkit","Nidhi","Naveen","Zalak"

                    };

     

                Data = items;

            }

            private List<string> _data;

            public List<string> Data

            {

                get

                {

                    return _data;

                }

                set

                {

                    _data = value;

                    NotifyPropertyChange("Data");

                }

     

            }

     

    Thanks,

    Rajnikant

    Saturday, September 25, 2010 1:20 PM
  • Adding the solution here as this post is coming on top of search result and this might help who ever is searching for similar issue.

    tonyuj's Blog: how to make button click work in listview on universal app ( xaml and WinRT)


    Tony Julien

    Thursday, April 9, 2015 12:01 AM