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

现在我的ExportCommand中的ViewModel为:
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();

但是我想从XAML作为gdv传递的值分配CommandParameter

最佳答案

RadGridView gdv = (RadGridView)param;
param是您发送的对象,因此只需将其转换为它的来源即可。

关于c# - 如何从 View 模型访问CommandParameter值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15088086/

10-16 18:36