0

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?

Remotec
  • 10,304
  • 25
  • 105
  • 147
  • 2
    Look into `ICommand` or my preference the `RelayCommand` which you should be able to find many examples of. – Khan Jul 29 '13 at 18:54
  • To supplement what @JefferyKhan is saying here is an example of what he is talking about http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030 – Bearcat9425 Jul 29 '13 at 18:59
  • To supplement what @Bearcat9425 said here's an even easier-to-read example: [Simplified MVVM Commanding with DelegateCommand](http://relentlessdevelopment.wordpress.com/2010/03/30/simplified-mvvm-commanding-with-delegatecommand/) :) – Arian Motamedi Jul 29 '13 at 19:01

1 Answers1

0

Using MvvmLight RelayCommand:

ViewModel:

private ICommand _runTestCommand;

public ICommand RunTestCommand()
{
    return _runTestCommand ?? (_runTestCommand = new RelayCommand(RunTest));
}

XAML:

<Button x:Name="btnRunTest" Command="{Binding Path=RunTestCommand}">
    Run Test
</Button>

See Also : How can I use the RelayCommand in wpf?

Community
  • 1
  • 1
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92