0

I've used CaliburnMicro to implement data binding for my data grid:

<Window x:Class="TicketDemo.Views.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TicketDemo.Views"
        mc:Ignorable="d" FontSize="20"
        Title="ShellView" Height="450" Width="800"
        Closing="{Binding Path=OnClose}">
    <Grid>
        <DataGrid x:Name="Tickets" AlternatingRowBackground="LightGray" CanUserAddRows="True"
                  AutoGenerateColumns="False"
                  >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Title" Binding="{Binding Path=Title}"/>
                <DataGridTextColumn Header="Content" Binding="{Binding Path=Content}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

I tried to use the same data binding to link a method called OnClose() to the Closing event.

using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using TicketDemo.Models;

namespace TicketDemo.ViewModels
{
    class ShellViewModel : Screen
    {
        public BindableCollection<TicketModel> Tickets { get; set; }

        public ShellViewModel()
        {
            Tickets = SQLiteDataAccess.LoadTickets();
        }

        public void OnClose(object sender, EventArgs e)
        {
            MessageBox.Show("Window Closing");
        }
    }
}

When I debug the program, I get this message:

using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TicketDemo.Views
{
    /// <summary>
    /// Interaction logic for ShellView.xaml
    /// </summary>
    public partial class ShellView : Window
    {
        public ShellView()
        {
            InitializeComponent();

            MessageBox.Show("Hello");
        }
    }
}

System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '9' and line position '9'.'

InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.

The SO post answering a question about this exception relates to buttons, but this is an event, so I don't think it's helpful to resolve my problem.

9
  • Please post code as text, not as image. Commented Jun 14, 2020 at 9:52
  • you should replace Closing="{Binding Path=OnClose}" with this Closing="OnClose" Commented Jun 14, 2020 at 10:39
  • @FerasSalim This will not work as OnClose is in the ViewMode, not the View Commented Jun 14, 2020 at 10:42
  • You can bind only to properties, not to methods Commented Jun 14, 2020 at 10:42
  • if you use view model, you should bind to properties not methods, you can create relay commands and bind it Commented Jun 14, 2020 at 10:45

1 Answer 1

1

if you are using caliburn, the event is declared like this:

replace Closing="{Binding Path=OnClose}"

by

cal:Message.Attach="[Event Closing] = [Action OnClose]"

(dont forget to add xmlns:cal="http://www.caliburnproject.org")

in ViewModel you write:

    public void OnClose()
    {
        MessageBox.Show("Window Closing");
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.