【问题标题】:MVVM Relay Command does not fire in Silverlight RIA appMVVM 中继命令不会在 Silverlight RIA 应用程序中触发
【发布时间】:2011-10-15 03:36:10
【问题描述】:

我正在开发 Silverlight 4 RIA(实体框架)应用程序,但在使用 MVVMLight RelayCommand 时遇到问题。我之前使用过它们没有问题,但是在我实现 ViewModelLocator 模式之后似乎出现了问题。

按钮控件上的绑定不会产生任何问题,应用程序会运行,但按钮单击不会触发 RelayCommand。

当我尝试在 Blend 中绑定 RelayCommand 时,它看不到 SelectionCommand 属性,但它可以看到其他属性,例如 IsBusy。

知道我做错了什么吗?

    <ListBox x:Name="ListBox" ItemsSource="{Binding Draws}" Background="Transparent">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Background="Transparent" HorizontalAlignment="Stretch" Orientation="Vertical" >
                    <telerik:RadButton Background="Transparent" Command="{Binding Path=SelectionCommand}" CommandParameter="{Binding Path=ID}"  >
                        <Image Source="{Binding Path=DrawImage, Converter={StaticResource imgConverter}, TargetNullValue=/NSPCCLotteryDraw;component/Assets/Images/JCBLogo.PNG}" Stretch="UniformToFill"/>
                    </telerik:RadButton>                      
                    <TextBlock Text="{Binding Path=DrawName}" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Times New Roman" FontSize="20" Margin="5,0,0,0" TextWrapping="Wrap" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <telerik:RadWrapPanel VerticalAlignment="Center" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.Command

Public Class DrawSelectorViewModel
    Inherits ViewModelBase

    Private _dataService As ILotteryDraw

    Private _draws As IEnumerable(Of Draw)

    Private _isBusy As Boolean

    Public Property SelectionCommand As RelayCommand(Of Int32)

    Public Property Draws As IEnumerable(Of Draw)
        Get
            Return _draws
        End Get
        Set(value As IEnumerable(Of Draw))
            _draws = value
            IsBusy = False
            RaisePropertyChanged("Draws")
        End Set
    End Property

    Public Property IsBusy As Boolean
        Get
            Return _isBusy
        End Get
        Set(value As Boolean)
            _isBusy = value
            RaisePropertyChanged("IsBusy")
        End Set
    End Property

    Public Sub New(dataService As ILotteryDraw)
        _dataService = dataService
        SetupCommands()
        LoadAvailableDraws()
    End Sub

    Private Sub SetupCommands()
        SelectionCommand = New RelayCommand(Of Int32)(AddressOf ShowLotteryDraw)
    End Sub

    Private Sub LoadAvailableDraws()
        IsBusy = True
        _dataService.GetActiveDraws(Sub(e) Draws = e)
    End Sub

    Private Shared Sub ShowLotteryDraw(drawID As Int32)
        Stop
    End Sub

End Class

【问题讨论】:

    标签: .net silverlight mvvm mvvm-light relaycommand


    【解决方案1】:

    我相信它会尝试在 Draw 实例上触发 SelectionCommand。您需要使用DataContextProxyrelative source binding 之类的东西。

    【讨论】:

    • 干杯 Derek,DataContextProxy 修复了它。
    • 在 SL5 中不需要它,因为您可以使用 RelativeSource。 goo.gl/0gcTM
    • 是的,我在第 9 频道的视频中看到了这一点。不过我们不会使用 BETA 软件,等待 RTM。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    相关资源
    最近更新 更多