Reputation: 69
good afternoon, I am writing this question to see if you can help me with a problem that I am having, it is probably easy to solve but I have not been able to make it work for days.
WPF programming I created an itemscontrols where I list a list with all the books I have, until there everything works perfect, my problem is that I want to do that when I press a button I take the value of the name of the book to use it in another part.
Then I leave the fragment of code that I have so that I can understand a little more
<StackPanel Name="stkMain">
<ItemsControl Name="itmCntrl">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" ></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Name="stk">
<materialDesign:Card Width="300" Margin="10" VerticalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Name="img" Source="{Binding PhotoPath}" Height="300" Width="240" Stretch="Fill" Cursor="Hand" />
<Button Grid.Row="1" Grid.Column="1" Style="{DynamicResource MaterialDesignFlatButton}" Content="MORE"
HorizontalAlignment="Right" Margin="8" Click="Button_Click"/>
<Label x:Name="nan" Content="{Binding Name}"></Label>
</Grid>
</materialDesign:Card>
</materialDesign:TransitioningContent>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
As you can see for each item created I have an image, a label and a button, and basically what I can not do is that by pressing the button I take the value of the label
I hope I can lend a hand with this.
Thank you.
Upvotes: 2
Views: 229
Reputation: 35646
in a Button_Click
method sender
parameter is a Button which has DataContext
ptoperty. DataContext
is a single object with Name
property:
// c#
Button b = (Button)sender;
object dc = b.DataContext;
//// cast dc to correct type, e.g.
// Book book = (Book)dc;
// string name = book.Name;
Upvotes: 2