Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C WPF:当窗口关闭时,如何调用具有数据绑定的方法?_C#_Wpf_Caliburn.micro - Fatal编程技术网

C# C WPF:当窗口关闭时,如何调用具有数据绑定的方法?

C# C WPF:当窗口关闭时,如何调用具有数据绑定的方法?,c#,wpf,caliburn.micro,C#,Wpf,Caliburn.micro,我已经使用CaliburnMicro为我的数据网格实现了数据绑定: <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

我已经使用CaliburnMicro为我的数据网格实现了数据绑定:

<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>
我尝试使用相同的数据绑定将一个名为OnClose的方法链接到关闭事件

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");
        }
    }
}
调试程序时,我收到以下消息:

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:提供上的值 “System.Windows.Data.Binding”引发异常。“行号为“9”和 行位置'9'

InvalidCastException:无法强制转换类型为的对象 “System.Reflection.RuntimeEventInfo”以键入 “System.Reflection.MethodInfo”


与按钮相关,但这是一个事件,因此我认为这对解决我的问题没有帮助。

如果使用caliburn,则事件声明如下:

替换关闭={Binding Path=OnClose}

cal:Message.Attach=[事件关闭]=[操作关闭]

别忘了添加xmlns:cal=http://www.caliburnproject.org

在ViewModel中,您可以编写:

    public void OnClose()
    {
        MessageBox.Show("Window Closing");
    }

请以文本而不是图像的形式发布代码。应使用此关闭替换关闭={Binding Path=OnClose}=OnClose@FerasSalim这将不起作用,因为OnClose处于ViewMode(视图模式)中,而不是ViewMode(视图模式)。您只能绑定到属性,而不能绑定到方法。如果使用视图模型,则应绑定到属性而不是方法,您可以创建中继命令并绑定它