简体   繁体   中英

“specified cast is not valid” GalaSoft.MvvmLight.Command.RelayCommand`1.Execute(Object parameter)

I'm pretty new to using MVVM Light so hopefully this is a simple fix, although I have spent most of the day trying to track down an answer :-(

In my xaml

<sdk:DataGrid Name="m_dgResults" AutoGenerateColumns="False" IsReadOnly="True" AreRowDetailsFrozen="True" SelectionMode="Single" ItemsSource="{Binding SearchResults}" >

.
.
.

<Button x:Name="cmdFTSViewText" Width="24" Height="24" Margin="5"  ToolTipService.ToolTip="View Text">
  <Image Source="../Images/ViewDocumentText.png" Stretch="None"/>
  <i:Interaction.Triggers>
      <i:EventTrigger EventName="Click">
          <cmd:EventToCommand Command="{Binding Source={StaticResource Locator}, Path=FindModel.ViewDocumentTextCommand}" 
                              CommandParameter="{Binding iUniqueID}"/>
      </i:EventTrigger>
  </i:Interaction.Triggers>
 </Button>

In my ViewModel

public RelayCommand<int> ViewDocumentTextCommand { get; private set; }
public FindModel() 
{
    .
    .
    .
    ViewDocumentTextCommand = new RelayCommand<Int32>(p => { ViewDocumentText(p); });
}

public void ViewDocumentText(Int32 iDocumentID)
{
    .
    .
    .
}

SearchResults.iUniqueID is an Int32

For some reason this is throwing the above exception when the button is pressed.

Thanks

Perhaps it's because your RelayCommand is defined as a RelayCommand<int> and you're trying to assign a RelayCommand<Int32> to it

Try changing your command definition to a simple ICommand instead.

It could also be that the value is null prior to the initial binding. Try using an object instead and not specifying the type.

new RelayCommand(p => ViewDocumentText(p));

and

public void ViewDocumentText(object iDocumentID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM