I have a ListBox bound to an ObservableCollection:
<ListBox Name="ListBoxItemsList">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0" FlowDirection="RightToLeft">
<Button Content="Add me!" Click="AddItem" />
<TextBlock Text="{Binding Path=name}" />
<TextBlock Text="{Binding Path=description}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The ObservableCollection objects have several properties, including uniqueID, name, description, colour, flavour. Depending on the selected item in the ListBox, I would like to populate some Labels with the selected item’s properties. Additionally, each item’s Button should perform a unique action (i.e. add one of the relevant items to an Array) however I cannot figure out how to send a unique argument to the AddItem() method. I thought that this would be a common use case, but I cannot find anything by googling.
Thanks.
You could bind the ListBox.SelectedItem to a DependencyProperty and then just bind that property to those Labels.
You could add the Item id to the Button.Tag
Then just get the button tag on the sender
Edit: You can also bind the full Item instead of just the Id to the button Tag.