【问题标题】:Passing Object in parameter MVVM Caliburn在参数 MVVM Caliburn 中传递对象
【发布时间】:2016-04-12 19:28:14
【问题描述】:

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

我试过了,它所做的只是将我的视图模型作为参数而不是对象本身传递。

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

【问题讨论】:

  • 向 ViewModel 发送控件不是处理问题的正确方法。 View Models 不应该知道任何特定的 UI 元素,因为理论上,您的 ViewModel 应该在 WPF 应用程序以及您的移动应用程序、Web 应用程序等中以相同的方式工作。告诉我们更多关于您的问题,也许是更好的解决方案会弹出。 :)
  • 当然,我正在使用 Telerik 控件。我正在尝试使用 MVVM 导出我的网格视图。我有导出方法,但我需要 radgridview 对象
  • 听起来像是一种行为。创建将注册到 RadGridView 的 DoubleClick 事件并将其导出到 excel 的行为。这样,您的 ViewModel 将不知道任何 UI 元素,而位于 UI 端的行为将知道您的 RadGridView 并能够执行您所需的所有逻辑。如果您需要示例,请告诉我。
  • @AlexPshul 一个例子将不胜感激 =)
  • 将在几分钟内发布答案

标签: c# wpf mvvm caliburn


【解决方案1】:

所以你需要两件事:

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

行为

创建自定义行为。因为我没有安装 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 中具有导出路径的您自己的属性。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-05-04
        • 2022-10-14
        • 2017-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多