views:

262

answers:

1

I have a simple test app in Silverlight 3 and Prism where I'm just trying to bind a button Click to a simple command I have created on a view model. This is a test app just to get commanding working. When I run it I get a binding error telling me that the view cannot find the command:

System.Windows.Data Error: BindingExpression path error: 'MyCommand' property not found on 'Bind1.ShellViewModel' 'Bind1.ShellViewModel' (HashCode=8628710). BindingExpression: Path='MyCommand' DataItem='Bind1.ShellViewModel' (HashCode=8628710); target element is 'System.Windows.Controls.Button' (Name=''); target property is 'Command' (type 'System.Windows.Input.ICommand')..

Here's my Shell view:

<UserControl 
x:Class="Bind1.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Commands="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation" 
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <TextBlock Text="Hello World!"></TextBlock>
        <Button Content="{Binding ButtonLabel}" Commands:Click.Command="{Binding Path=MyCommand}" />
    </StackPanel>
</Grid>
</UserControl>

In the constructor of the view I instantiate a ViewModel (I'm not worried about using the container yet...):

public partial class ShellView : UserControl
{
    public ShellView()
    {
        InitializeComponent();
        DataContext = new ShellViewModel();
    }
}

Here's my ViewModel:

public class ShellViewModel
{
    public string ButtonLabel { get { return "DoIt!!"; } }
    public DelegateCommand<object> MyCommand = new DelegateCommand<object>(ExecuteMyCommand);
    public static void ExecuteMyCommand(object obj)
    {
        Debug.WriteLine("Doit executed");
    }
}

The button label binding works fine so the view is finding the ViewModel OK.

Why can't it find MyCommand? It's driving me mad - I am obviously doing something simple very wrong...

Thanks a lot.

A: 

What an idiot... Sorry for wasting your time.

I forgot to make MyCommand a property!!! Too much staring at the screen. It was just a public field so the binding infrastructure couldn't see it. All is well now.

Michael