C# WPF – Using Buttons in ItemsControl, how to let the Command know which instance of the button has been clicked?

Solution (Edit):

In the button-command, I have to provide that the Relative Source AncestorType=ItemsControl. This binds the button(s) to the same ItemsSource that the ItemsControl has (i suppose), which is Items.

<Button Content="Display Message" Height="25" 
        Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.DisplayMessageCommand}"/>

In the ViewModel, the RelayCommand and corresponding function can be defined like this. I have to specify that the parameter is an ItemModel. Then I can call the properties of the ItemModel as shown in DisplayMessage()

public RelayCommand DisplayMessageCommand => new RelayCommand(parameter => DisplayMessage((ItemModel)parameter), parameter => true);

private void DisplayMessage(ItemModel item)
    {
        MessageBox.Show(item.ItemMessage); 
    }

My question is: When I have a button within an ItemsControl, how do I let the command know which instance of the button has been clicked?

I have included a simple example below, where i have a Textblock+Button in an ItemsControl.

The ItemsSource of the ItemsControl is an ObservableCollection of ItemModel. When i click a button, i want to tell the corresponding command which ItemModel the button belonged to, and display its ItemMessage in a MessageBox.

How do i do that?

This is what I have so far:

MainWindow.xaml

<Window x:Class="TestApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="200">
    <StackPanel Margin="20">
        <ItemsControl ItemsSource="{Binding Items}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0 0 0 5">
                        <TextBlock Margin="0 0 20 0" Text="{Binding ItemName}"/>
                        <Button Content="Display Message" Height="25" 
                                Command="{Binding DisplayMessageCommand}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        ItemViewModel viewModel = new ItemViewModel();
        this.DataContext = viewModel;
        InitializeComponent();
    }
}

ItemModel.cs

public class ItemModel
{
    public string ItemName { get; set; }
    public string ItemMessage { get; set; }

    public ItemModel() { }
}

ItemViewModel.cs

public class ItemViewModel
{
    public ObservableCollection<ItemModel> Items { get; set; }

    public ItemViewModel()
    {
        Items = new ObservableCollection<ItemModel>()
        {
            new ItemModel {ItemName = "Item 1", ItemMessage = "This is item 1"},
            new ItemModel {ItemName = "Item 2", ItemMessage = "This is item 2"},
            new ItemModel {ItemName = "Item 3", ItemMessage = "This is item 3"}
        };
    }

    public RelayCommand DisplayMessageCommand => new RelayCommand(execute => DisplayMessage(), canExecute => true);

    private void DisplayMessage()
    {
        //I want the MessageBox to display the ItemMessage that the clicked button corresponds to
        MessageBox.Show("..."); 
    }
}

RelayCommand.cs

public class RelayCommand : ICommand
{
    private Action<object> _execute;
    private Func<object, bool> _canExecute;

    public event EventHandler? CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
    {
        this._execute = execute;
        this._canExecute = canExecute;
    }

    public bool CanExecute(object? parameter)
    {
        return _canExecute == null || _canExecute(parameter);
    }

    public void Execute(object? parameter)
    {
        _execute(parameter);
    }
}

9

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật