tags:

views:

47

answers:

4

I have a collection that I am displaying in a WPF Listview. I will have an edit button in each row and need to pass an ID to another control on the screen when that's clicked. I'm not going to be editing in place so I'm not using the Gridview.

How do I pass this ID to my other control?

Currently my XAML looks like this.

<ListView Name="uxPackageGroups" ItemsSource="{Binding PackageGroups}" BorderThickness="0" Grid.Row="6" Grid.Column="1" Width="300" BorderBrush="#FF0000E8">
<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel Name="uxStackPanel" Orientation="Horizontal">
            <Button Content="Edit" Width="50" />
            <Label Content="{Binding Name}" Height="20" Margin="0" Padding="0"/>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

This WPF newbie thanks you in advance!

+1  A: 

You're using databinding, so it's pretty easy. In the code that is responding to the button click, get a reference to the button and examine its DataContext property. That will have a reference to the underlying object that you bound it to.

protected void EditButton_Click(object sender, RoutedEventArgs e)
{
   TextBox textBox = (TextBox)sender;

   int id =  ((TheBounObjectType)textBox.DataContext).Id;
}
RQDQ
+1  A: 

Command parameters are good for this:

 <Button Content="Edit" Width="50" 
      Command="{some command here}" 
      CommandParameter="{Binding ID}" />

For the "some command here" part, make sure you are declaring a command.

Philip Rieck
A: 

If you don't want to go as far as creating commands to do it, you could use the "Tag" property of the button:

<Button Content="Edit" Width="50" Tag={Binding} />

or

<Button Content="Edit" Width="50" Tag={Binding ID} />

and then reference the button's tag property in your event handler.

John Gardner
A: 

I neglected to mention that I'm using MVVM so this is a touch tricker. Here's what I have so far but my command doesn't fire.

<ListView Name="uxPackageItems" ItemsSource="{Binding Path=PackageItems, UpdateSourceTrigger=PropertyChanged}">
<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel Name="uxStackPanel" Orientation="Horizontal">

          <telerik:RadButton Name="uxRemovePackageItem" Content="Remove from package" 
             Command="{Binding RemovePackageItemCommand, UpdateSourceTrigger=PropertyChanged}" 
             DataContext="{Binding}" CommandParameter="{Binding PackageItemID}"/>

          <Label Content="{Binding Item.Name}" Height="20" Margin="0 0 4 0" Padding="0"/>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

And my code from my ViewModel

public RelayCommand RemovePackageItemCommand
{
    get
    {
    return new RelayCommand(
       () => RemoveItemCommand_Execute());
    }
}

private void RemoveItemCommand_Execute()
{
   MessageBox.Show("Add code to remove data");
}

If I put the button in the XAML outside of my ListView I can call the remove command. Obviously that doesn't help as I need the id from the row of my ListView.

Thanks.

engwar