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???
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"/>
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-mv-vm/
It will work on Windows 8 also, as it is a feature of XAML.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.