2010-02-24 18 views
2

J'ai une application de test simple dans Silverlight 3 et Prism où j'essaye juste de lier un bouton Cliquez sur une commande simple que j'ai créée sur un modèle de vue . Ceci est une application de test juste pour obtenir le travail de commande. Quand je le lance je reçois une erreur de lien me disant que la vue ne peut pas trouver la commande:Pourquoi je ne peux pas lier mon bouton Silverlight Cliquez sur un Prism DelegateCommand

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')..

Voilà mon avis de Shell:

<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> 

Dans le constructeur de la vue que j'instancier un ViewModel (Je ne suis pas inquiet à propos de l'utilisation du conteneur encore ...):

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

Voici mon 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"); 
    } 
} 

La liaison d'étiquette de bouton fonctionne correctement et la vue trouve le ViewModel OK. Pourquoi ne trouve-t-il pas MyCommand? Ça me rend fou - je fais évidemment quelque chose de très mal ...

Merci beaucoup.

Répondre

2

Quel idiot ... Désolé de perdre votre temps. J'ai oublié de faire de MyCommand une propriété !!! Trop regarder fixement l'écran. C'était juste un champ public donc l'infrastructure de liaison ne pouvait pas le voir. Tout va bien maintenant.