2013-02-26 80 views
0

我在視圖按鈕:如何從視圖模型訪問CommandParameter值

<Button Grid.Row="0" Grid.Column="1" Content="Export to CSV " 
     HorizontalAlignment="Right" VerticalAlignment="Center" 
     Margin="2,2,20,2" Style="{StaticResource ExportButton}" 
     Command="{Binding ExportToExcelCommand}" 
     CommandParameter="{TelerikGrid}" 
/> 

現在,我有我的ViewModel作爲ExportCommand

private RelayCommand _exportToExcelCommand; 
public ICommand ExportToExcelCommand 
{ 
    get 
    { 
     if (_exportToExcelCommand == default(RelayCommand)) 
     { 
      _exportToExcelCommand = new RelayCommand(ExportToExcel, CanExport); 
     } 
     return _exportToExcelCommand; 
    } 
} 

private void ExportToExcel(Object param) 
{ 
    try 
    { 
     //ToDo: Update With Command Parameters 
     RadGridView gdv = new RadGridView(); 

     using (Stream stream = dialog.OpenFile()) 
     { 
      gdv.Export(Columns); 
     } 
    } 
    catch (FaultException ex) 
    { 
     var faultMessage = ex.Reason.GetMatchingTranslation().Text + "/n" + ex.StackTrace; 
    } 
} 

現在我創建一個新的實例RadGridView:

RadGridView gdv = new RadGridView(); 

但是相反,我想分配gdv從作爲CommandParameter從XAML

回答

3
RadGridView gdv = (RadGridView)param; 

param傳遞的值是您發送的對象,所以纔將它轉換爲它的起源。