tags:

views:

104

answers:

2

I have a ListBox that has a custom DataTemplate assigned to it, so that it can correctly display it's content - a custom "Accessory" (which consists of three string properties) object per row. Additionally, there is a button on each row. That button should trigger an event that adds the selected accessory to a MemoryList. Here is the DataTemplate:

    <DataTemplate x:Key="AccessoryListBoxTemplate">
        <StackPanel>
             <!--Truncated-->
                    <TextBlock FontFamily="Avenir Next LT Pro" VerticalAlignment="Center" FontSize="14" Text="{Binding Path=AgilityHeader}" Margin="3,0,0,0" Grid.Column="0" />
                    <TextBlock FontFamily="Avenir Next LT Pro" VerticalAlignment="Center" FontSize="14" Text="{Binding Path=ItemNumber}" Grid.Column="1" />
                    <TextBlock FontFamily="Avenir Next LT Pro" HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding Path=Price}" FontSize="14" Grid.Column="2" />
                    <Button x:Name="ButtonAccessoryAddToMemoryList" VerticalAlignment="Center" Click="buttonAccessoryAddToMemoryList_Click" HorizontalAlignment="Right" FontSize="14" Width="80" Grid.Column="3" Margin="0,5,0,5">Minneslista</Button>
                </Grid>
            </StackPanel>
        </StackPanel>
    </DataTemplate>

And here is the ListBox:

<ListBox Grid.ColumnSpan="3" Grid.Row="1" BorderThickness="0" x:Name="ListBoxAccessories" ItemTemplate="{StaticResource AccessoryListBoxTemplate}" HorizontalContentAlignment="Stretch" ItemsSource="{Binding}" SelectedIndex="-1" IsEnabled="True" />

The problem I'm having is this - I cannot reliably identify on which row ButtonAccessoryAddToMemoryList is clicked, since the row that the button is on is not set as the SelectedItem for the ListBox if the user doesn't first select the row and then push the button - and honestly, who does that? :)

How should I go about identifying which button was pressed? Any help would be greatly appreciated. Thanks!

[EDIT] Thanks to Chadwick for that answer. Works perfectly. [/EDIT]

+1  A: 
  1. Try out M-V-VM pattern and a command binding. If the datatemplate were bound to its own object and the command were activated, then you would already know the record being clicked.

  2. In the XAML, you could give the button a _Loaded event handler. Then, in the code behind, set the _Click event. Perhaps make an array of delegates so that the clicks will call different handlers.

Jarrett Meyer
Good reference for MVVM: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Nidonocu
+1  A: 

If what you're really after is to know which Accessory object was clicked on you can set the Tag property of the button:

<Button x:Name="ButtonAccessoryAddToMemoryList" Tag="{Binding}" Click="buttonAccessoryAddToMemoryList_Click" ... >Minneslista</Button>

and then cast out the object in the click handler:

private void ButtonAccessoryAddToMemoryList(object sender, RoutedEventArgs e)
{
    Button b = e.Source as Button;
    Accessory a = b.Tag as Accessory;
Chadwick
That worked nicely. Neat and tidy solution. Perfect!
Ciddan