Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML arguments for Click event

Tags:

c#

events

xaml

I have the following XAML:

<Button Name="btnJeans" Click="btnJeans_Click" Padding="-1" >
    <StackPanel Orientation="Horizontal" Margin="0,0,0,17" Name="jeansItem">
        <!--Replace rectangle with image-->
        <Image Height="119" Width="82" Source="{Binding Image}" Margin="12,0,9,0"/>
        <StackPanel Width="311">                                    
            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
            <TextBlock Text="{Binding Price}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
        </StackPanel>
    </StackPanel>
</Button>

However, btnJeans_Click needs to pass "{Binding Name}" as an argument.

How would I do this? I'm developing for Windows Phone 7.

like image 815
Tim van Dalen Avatar asked Feb 26 '23 01:02

Tim van Dalen


2 Answers

An alternative on the pc is to get get the DataContext from the click event args e.g.

private void Button_Click(object sender, RoutedEventArgs e)
    {
      var record = ((Button)e.OriginalSource).DataContext;
      MessageBox.Show("Do something with " + record.ToString()); 
    }

this might be applicable to the phone as well?

like image 139
jk. Avatar answered Mar 08 '23 16:03

jk.


I think you need to use a command instead of a button click event to do this so you can do:

<Button Name="btnJeans" Command="{Binding Path=ButtonClickCommand}" CommandParameter="{Binding Name}">
    ...
</Button>

You'll then need to expose ButtonClickCommand as a ICommand object.

How to do this will depend on the structure of the rest of your application. E.g. Using MVVM pattern etc.

like image 37
Andy Lowry Avatar answered Mar 08 '23 16:03

Andy Lowry