简体   繁体   English

如何从视图模型访问CommandParameter值

[英]How To Access CommandParameter value from View Model

I have a Button in the view: 我在视图中有一个按钮:

<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}"
/>

Now I have the ExportCommand in my ViewModel as : 现在我在ViewModelExportCommand

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;
    }
}

Now I am creating a new instance of RadGridView: 现在,我正在创建RadGridView的新实例:

RadGridView gdv = new RadGridView();

But instead I want to assign gdv from the value that is passed as CommandParameter from XAML 但是相反,我想从XAML作为CommandParameter传递的值分配gdv

RadGridView gdv = (RadGridView)param;

param是您发送的对象,因此只需将其投射到其原点即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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