I am trying to bind a button click event in a bound ListView to a method on the list's underlying data source. I've searched for this but all of the examples seem to list pages of code and I'm pretty sure this can be achieved with a binding expression - or at least I hope so!
The underlying data source is a collection of this class...
public class AppTest : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
int _priority;
string _testName;
public void RunTest()
{
// TODO: Implement
}
public int Priority
{
get { return _priority; }
set
{
_priority = value;
NotifyPropertyChanged("Priority");
}
}
public string TestName
{
get { return _testName; }
set
{
_testName = value;
NotifyPropertyChanged("TestName");
}
}
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
... and the XAML looks like this ...
<Window.Resources>
<CollectionViewSource x:Key="cvs" x:Name="cvs" Source="">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Priority" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<ListView x:Name="TestList" ItemsSource="{Binding Source={StaticResource cvs}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Application:AppTestControl Message="{Binding TestName}" />
<Button x:Name="btnRunTest">
Run Test
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListView>
</Grid>
How can I bind the click of btnRunTest
to the RunTest()
method in the bound underlying object?