为什么我不能将 Silverlight 按钮单击绑定到 Prism DelegateCommand

发布于 2024-08-22 13:37:57 字数 1868 浏览 6 评论 0原文

我在 Silverlight 3 和 Prism 中有一个简单的测试应用程序,我只是想将按钮 Click 绑定到我在视图模型上创建的简单命令。 这是一个测试应用程序,只是为了让命令正常工作。 当我运行它时,我收到一个绑定错误,告诉我视图找不到该命令:

System.Windows.Data 错误:BindingExpression 路径错误:在“Bind1.ShellViewModel”“Bind1.ShellViewModel”上找不到“MyCommand”属性 (HashCode=8628710)。 BindingExpression: Path='MyCommand' DataItem='Bind1.ShellViewModel' (HashCode=8628710);目标元素是“System.Windows.Controls.Button”(名称=“”);目标属性是“Command”(类型“System.Windows.Input.ICommand”)..

这是我的 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>

在视图的构造函数中,我实例化了一个 ViewModel(我还不担心使用容器...) :

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

这是我的 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");
    }
}

按钮标签绑定工作正常,因此视图找到 ViewModel 正常。

为什么找不到 MyCommand?这让我发疯 - 我显然在做一些简单的非常错误的事情......

非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2024-08-29 13:37:57

真是个白痴……抱歉浪费了你的时间。

我忘记将 MyCommand 设置为属性!!!盯着屏幕看太多了。
它只是一个公共字段,因此绑定基础设施看不到它。
现在一切都很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文