2011-07-28 48 views
2

我正在開發Silverlight 4 RIA(實體框架)應用程序,並且遇到了使用MVVMLight RelayCommand的問題。我以前用過它們沒有問題,但是在實現了ViewModelLocator模式後,似乎存在一個問題。MVVM中繼命令在Silverlight RIA應用程序中未觸發

按鈕控件上的綁定不會生成問題,應用程序將運行,但按鈕單擊不會觸發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

我相信它會嘗試上繪製的實例火SelectionCommand。你需要使用類似DataContextProxyrelative source binding

+0

Cheers Derek,DataContextProxy修復了它。 –

+1

在SL5中它不會被需要,因爲您可以使用RelativeSource。 http://goo.gl/0gcTM –

+0

是的,我在頻道9的視頻中看到過。我們不會使用BETA軟件,等待RTM。 –

相關問題