I am developing a Silverlight 4 RIA (Entity framework) application and I am having issues using a MVVMLight RelayCommand. I have used them before without problem but there seems to be an issue after I have implemented the ViewModelLocator pattern.
The bindings on the button control generates no issues and the application runs but the button click does not fire the RelayCommand.
When I try to bind the RelayCommand in Blend it can not see the SelectionCommand property but it can the others such as IsBusy.
Any idea what I am doing wrong?
<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>
开发者_C百科 <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
I believe it will try and fire SelectionCommand on an instance of Draw. You need to use something like the DataContextProxy or a relative source binding.
精彩评论