2010-02-24 64 views
2

我在Silverlight 3和Prism中有一個簡單的測試應用程序,我只是試圖綁定一個按鈕點擊我在視圖模型上創建的簡單命令。 這是一個測試應用程序,只是爲了指揮工作。 當我運行它,我得到一個綁定錯誤告訴我,認爲找不到命令:爲什麼我不能綁定我的Silverlight按鈕點擊到棱鏡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')..

這裏是我的殼牌觀點:

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

在視圖的構造我實例化一個視圖模型(我並不擔心使用容器尚未...):

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

這裏是我的視圖模型:

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

按鈕標籤綁定工作正常,所以視圖正在查找ViewModel OK。

爲什麼找不到MyCommand?這讓我很生氣 - 我明顯在做一件簡單的事情非常錯誤......

非常感謝。

回答

2

什麼是白癡...對不起,浪費你的時間。

我忘了讓MyCommand成爲一個屬性!太盯着屏幕。 這只是一個公共領域,所以綁定的基礎設施看不到它。 現在一切都很好。

相關問題