Silverlight 3.0 Silverlight 3.0和MVVM模式

Silverlight 3.0 Silverlight 3.0和MVVM模式,silverlight-3.0,Silverlight 3.0,我正在写这篇文章,他们正在使用Silverlight4。 但是我使用的是Silverlight 3。但是对于button,我们无法找到命令 Command=“{Binding Path=DataContext.GetPerson,ElementName=LayoutRoot}” <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="53,112,0,0" Name="button1"

我正在写这篇文章,他们正在使用Silverlight4。

但是我使用的是Silverlight 3。但是对于button,我们无法找到命令

Command=“{Binding Path=DataContext.GetPerson,ElementName=LayoutRoot}”

<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="53,112,0,0" Name="button1"
                VerticalAlignment="Top" Width="75"  Command="{Binding Path=DataContext.GetPerson, ElementName= LayoutRoot }" 
                    CommandParameter="{Binding Path=Age, ElementName=hi}"  />

那么在Silverlight3中,我应该怎么做才能获得按钮的这个命令属性呢


任何帮助都很好

命令是将操作与控件(例如按钮)关联的可绑定方式。 该命令控制两件事:

  • 是否可以执行
  • 执行时该怎么办
通过实现接口

它们在WPF中已经存在了一段时间,最近被添加到SL4中

如果希望代码在SL3中工作,则必须将IsEnabled属性绑定到视图模型中与命令的CanExecute实现具有相同逻辑的属性 为按钮添加一个Click事件处理程序,该按钮具有与命令的Execute相同的逻辑


此外,此按钮将参数传递给命令(CommandParameter),您必须手动处理传递此参数的操作

您可以使用类似的框架,将相关dll添加到项目中,然后向页面顶部添加引用,如下所示

xmlns:prism="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
然后在按钮上声明监狱属性

<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="53,112,0,0" Name="button1"
        VerticalAlignment="Top" Width="75"  Command="{Binding Path=DataContext.GetPerson, ElementName= LayoutRoot }" 
        prism:Click.Command={Binding SomeCommand}
        prism:Click.CommandParameter="{Binding Path=Age, ElementName=hi}"  />