0

I'm wondering how would I pass my gridview object as a parameter into my caliburn micro event.

I tried this and all it did was pass my viewmodel as the parameter instead of the object itself.

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


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

Here's one possible solution, I have to add an onclick event but maybe there's a better solution?

private void ReportGridView_OnMouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    (this.DataContext as LoanGridViewModel).RunReport(LoanGrid); 
}
Ilan
  • 2,762
  • 1
  • 13
  • 24
Master
  • 2,038
  • 2
  • 27
  • 77
  • Sending a control to the ViewModel is not the correct way to handle your problem. View Models shouldn't know any specific UI elements, since your ViewModel, in theory, should work the same in the WPF application, as well on your Mobile application, Web application and etc. Tell us more about your problem, maybe a better solution will pop up. :) – Alex Pshul Apr 12 '16 at 19:33
  • Sure, I'm using telerik controls. I'm trying to excel export my gridview using MVVM. I have the method to export but I need the radgridview object – Master Apr 12 '16 at 19:34
  • Sounds like a job for a behavior. Create a behavior that will register to the _DoubleClick_ event of the RadGridView and will export it to excel. This way, your ViewModel won't know any UI element, while the behavior, which IS on the UI side, will know your RadGridView and will be able to perform all your needed logic. Tell me if you need am example. – Alex Pshul Apr 12 '16 at 19:54
  • @AlexPshul An Example would be greatly appreciated =) – Master Apr 12 '16 at 19:55
  • Will post an answer in several minutes – Alex Pshul Apr 12 '16 at 19:59
  • There's one way I can do it, add a click event and have something like this private void ReportGridView_OnMouseDoubleClick(object sender, MouseButtonEventArgs e) { (this.DataContext as LoanGridViewModel).RunReport(LoanGrid); } – Master Apr 12 '16 at 20:01
  • Put your logic of RunReport method inside the behavior. – Alex Pshul Apr 12 '16 at 20:17

3 Answers3

0

So you'll need two things:

  1. The behavior itself
  2. Use the behavior from XAML.

Behavior

Create a custom behavior. Since I don't have telerik installed, I've called to methods that probably won't compile. You will need to switch the method names to the relevant methods.

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

After the behavior is created, import the namespace in the xaml, and use it in the telerik grid.

XAML

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

And of course, just change the binding to your own property in ViewModel that has the path for export.

Feel free to update on your progress. Happy Coding! :)

Alex Pshul
  • 694
  • 3
  • 11
0

You can pass the sender ot the event arguments to the method in Caliburn. Just change [Event MouseDoubleClick] = [RunReport(LoanGrid)] to [Event MouseDoubleClick] = [RunReport($source)].

For more info about actions take a look at https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118
0

You can pass the sender of the event arguments to the method in Caliburn.Micro. [Event MouseDoubleClick] = [RunReport($this)].

Here is the answer. You can find the good answer and please read the comment

DevCaf
  • 109
  • 11