Reputation: 23
I need a little help here. I have 40 buttons on a Master-Details MVVM sketch. As you know, I tap a button from MainPage and it displays data on DetailsPage. What I need to do is to tap on any button (1,2,3...40) and it to send its name (Semana 1, 2,...,40) as the header of the Details Page. I can achieve this by naming each button
<StackPanel x:Name="Semanas1a5">
<Button x:Name="bs1" Content="Semana 1" Click="Button_Click_1"/>
<Button x:Name="bs2" Content="Semana 2" Click="Button_Click_2"/>
<Button x:Name="bs3" Content="Semana 3" Click="Button_Click_3"/>
<Button x:Name="bs4" Content="Semana 4" Click="Button_Click_4"/>
<Button x:Name="bs5" Content="Semana 5" Click="Button_Click_5"/>
</StackPanel>
Then passing its name as parameter within the navigation command.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/DataBoundApp1;component/MainPage.xaml?hdr=" + bs1.Content, UriKind.Relative));
}
However I think there must be a wiser way of doing it by INotifyPropertyChanged or anything else instead of writing a name for each one of the 40. Anyone has a suggestion? Thank you!
Upvotes: 1
Views: 54
Reputation: 2204
Instead of adding a Click
event for each button, why not have them all share the same event?
<StackPanel Button.Click="OnButtonClick">
<Button Content="Semana 1"/>
<Button Content="Semana 2"/>
<Button Content="Semana 3"/>
<Button Content="Semana 4"/>
<Button Content="Semana 5"/>
</StackPanel>
Then, you can use the RoutedEventArgs
to receive the OriginalSource
, from which you can get the Content
property!
private void OnButtonClick(object sender, RoutedEventArgs e)
{
Button clickedButton = e.OriginalSource as Button;
if (clickedButton != null)
NavigationService.Navigate(new Uri("/DataBoundApp1;component/MainPage.xaml?hdr=" + clickedButton.Content, UriKind.Relative));
}
Upvotes: 4