Reputation: 181
In Windows 8, C# + xaml i have some class
Class ABC
{
public string a {get; set;}
public void someMethod()
{
**some code, changing a**
}
}
and binding in xaml
<ListBox x:Name="playlistBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding a}"/>
<Button Name="removeAlbumBtn" Content="method" Click="**NEED BINDING TO SOMEMETHOD HERE**"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
So what i must type to call someMethod???
Upvotes: 0
Views: 4097
Reputation: 4303
You can do it using commands. But the amount of work you have to do is a bit more than just writing a method to bind it. This post on commands in WPF and Silverlight might help you in understanding and implementing commands: http://tsells.wordpress.com/2010/06/23/command-binding-with-wpf-and-silverlight-net-4-0-with-m-v-vm/
It will work on Windows 8 also, as it is a feature of XAML.
Upvotes: 0
Reputation: 39085
You need to handle the Click
event through a handler method in the code behind of that xaml file. For example, if the xaml you show is in a MyControl.xaml
. You'll have a method like the following in the MyControl.xaml.cs
:
private void removeAlbumBtn_Click(object sender, RoutedEventArgs e)
{
var a = ((Button)sender).DataContext as ABC;
if(a != null)
a.someMethod();
}
And change the xaml to be:
<Button Name="removeAlbumBtn" Content="method" Click="removeAlbumBtn_Click"/>
Upvotes: 1