Piano Telope
Piano Telope

Reputation: 137

C# WPF: How do I call a method with data binding when a window is closing?

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.

Upvotes: 0

Views: 670

Answers (1)

Frenchy
Frenchy

Reputation: 17007

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

Upvotes: 1

Related Questions