Search code examples
c#wpfuser-controlsrouted-commands

Get the item from list where the control pressed


I have created a sample application as follows.

UserControl1.xaml

<Grid Margin="0,0,-155,0">
    <Label Content="{Binding Username}" HorizontalAlignment="Left" Margin="23,23,0,0" VerticalAlignment="Top"/>
    <Button Content="Close this UC" HorizontalAlignment="Left" Margin="414,22,0,0" VerticalAlignment="Top" Width="119" Command="{Binding ButtonClicked}" />
    <ProgressBar HorizontalAlignment="Left" Value="{Binding Completion}" Height="10" Margin="204,23,0,0" VerticalAlignment="Top" Width="154"/>
</Grid>

I have included the usercontrol as list in my MainWindow

MainWindow.Xaml

<Grid>
    <ItemsControl x:Name="icTodoList">

        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type local:UserControl1}">

                <local:UserControl1 />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

Mainwindow.xaml.cs

    public partial class MainWindow : Window
    {
        ObservableCollection<TodoItem> items = new ObservableCollection<TodoItem>();
        public MainWindow()
        {
            InitializeComponent();


            RoutedCommand rc = new RoutedCommand();
            CommandBindings.Add(new CommandBinding(rc, Button_click));

            items.Add(new TodoItem() { Username = "Eric", Completion = 45, ButtonClicked = rc });
            items.Add(new TodoItem() { Username = "Maxwell", Completion = 80 });
            items.Add(new TodoItem() { Username = "Sarah", Completion = 60 });
            icTodoList.ItemsSource = items;
        }

        private void Button_click(object sender, ExecutedRoutedEventArgs e)
        {
            **//Need to get the Item where the event triggered**
        }
    }

    public class TodoItem
    {
        public string Username { get; set; }
        public int Completion { get; set; }
        public RoutedCommand ButtonClicked { get; set; }
    }

Problem: Button_Click event handler got triggered on clicking the button as expected. However, I am unable to get the item(sender) of type TodoItem where the button was clicked.


Solution

  • You have to set a CommandParameter for that button.

    Something like:

    <Button Content="Close this UC" HorizontalAlignment="Left" Margin="414,22,0,0" VerticalAlignment="Top" Width="119" Command="{Binding ButtonClicked}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid}}, Path=DataContext}"/>
    

    You can see that as a parameter I set the grid data context (which should be an item of type TodoItem)

    And then, in the RoutedCommand you can access it as e.Parameter