在参数MVVM Caliburn中传递Object

时间:2016-04-12 19:28:15

标签: c# wpf mvvm caliburn

我想知道如何将gridview对象作为参数传递给我的caliburn微事件。

我尝试了这一点,它所做的就是将我的viewmodel作为参数而不是对象本身传递。

<Setter Property="cal:Message.Attach" Value="[Event MouseDoubleClick] = [RunReport(LoanGrid)]"/>


<telerik:RadGridView x:Name="LoanGrid"  ...../>

这是一个可能的解决方案,我必须添加一个onclick事件,但也许有更好的解决方案?

private void ReportGridView_OnMouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    (this.DataContext as LoanGridViewModel).RunReport(LoanGrid); 
}

3 个答案:

答案 0 :(得分:0)

所以你需要两件事:

  1. 行为本身
  2. 使用XAML中的行为。
  3. 行为

    创建自定义行为。由于我没有安装telerik,我调用了可能无法编译的方法。您需要将方法名称切换为相关方法。

    public class ExportOnDoubleClickBehavior
    {
        // This is the attached property
        public static string GetExportPath(DependencyObject obj)
        {
            return (string)obj.GetValue(ExportPathProperty);
        }
    
        public static void SetExportPath(DependencyObject obj, string value)
        {
            obj.SetValue(ExportPathProperty, value);
        }
    
        // Using a DependencyProperty as the backing store for ExportPath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ExportPathProperty =
            DependencyProperty.RegisterAttached("ExportPath", typeof(string), typeof(ExportOnDoubleClickBehavior), new PropertyMetadata(string.Empty, PathChanged));
    
        // Here we are registering to the double click event of the grid.
        // Change the registration to the relevant event.
        private static void PathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RadGridView grid = d as RadGridView;
            if (RadGridView == null)
                throw new InvalidCastException("ExportOnDoubleClickBehavior can be set only on RadGridView.");
    
            // Change to relevant event HERE
            grid.DoubleClick += ExportGridToExcel;
        }
    
        // Here we will export the grid to excel.
        // Again, change to relevant method
        private static void ExportGridToExcel(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            RadGridView grid = sender as RadGridView;
            if (grid == null)
                throw new InvalidCastException("ExportOnDoubleClickBehavior can be set only on RadGridView.");
    
            string exportPath = GetExportPath(grid);
            grid.ExportToExcel(exportPath); // HERE!!!
        }
    }
    

    创建行为后,在xaml中导入名称空间,并在telerik网格中使用它。

    XAML

    <telerik:RadGridView x:Name="LoanGrid"
                         behaviors:ExportOnDoubleClickBehavior.ExportPath="{Binding ExportPathFromViewModel}"    ...../>
    

    当然,只需在具有导出路径的ViewModel中更改绑定到您自己的属性。

    随时更新您的进度。快乐的编码! :)

答案 1 :(得分:0)

您可以将事件参数的发件人传递给Caliburn中的方法。只需将[Event MouseDoubleClick] = [RunReport(LoanGrid)]更改为[Event MouseDoubleClick] = [RunReport($source)]

有关操作的详细信息,请查看https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions

答案 2 :(得分:0)

您可以将事件参数的发送者传递给 Caliburn.Micro 中的方法。 [Event MouseDoubleClick] = [RunReport($this)]。

这是答案。 You can find the good answer and please read the comment