MVVM中继命令不会在Silverlight RIA应用程序中触发

时间:2011-07-28 13:39:54

标签: .net silverlight mvvm mvvm-light relaycommand

我正在开发一个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

1 个答案:

答案 0 :(得分:1)

我相信它会尝试在Draw实例上激活SelectionCommand。您需要使用类似DataContextProxyrelative source binding的内容。

相关问题