如何处理客户路由事件

时间:2016-03-13 21:55:48

标签: c# wpf events event-handling

我有一个链接到EF MySql数据库模型的c#.net4项目。主窗口有一个数据网格,显示用户已添加到MySql数据库的“EnteredTickets”。当用户双击“EnteredTickets”之一的数据网格行时,会打开一个窗口(称为编辑),以允许用户编辑“已输入的故障单”,然后“保存”编辑。保存编辑完成后,“编辑”窗口将关闭。

我想要做的是刷新主窗口上的数据网格以显示编辑过的数据。

要在阅读后执行此操作,我相信我需要引发一个事件,然后在MainWindowxmal.cs中“处理”此事件。特别是我遵循here步骤。正如您将看到的,我已经添加了一个消息框,以便从编辑窗口中了解事件。但是我没有在主窗口中正确处理事件,因为我在事件处理程序中添加的消息框没有显示“编辑完成”,这是我的代码

在编辑窗口中

cublasStatus_t cublasSetMatrix(int rows, int cols, int elemSize,
                        const void *A, int lda, void *B, int ldb)

通过以下代码点击“保存”按钮引发事件,我收到消息框“提升路由”

public partial class EditTicket : Window
{

    //public event EventHandler EditFinished;
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent EditFinishedEvent = EventManager.RegisterRoutedEvent(
   "EditFinished", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(EditTicket));

 // Provide CLR accessors for the event
    public event RoutedEventHandler EditFinished
    {
        add { AddHandler(EditFinishedEvent, value); }
        remove { RemoveHandler(EditFinishedEvent, value); }
    }
// This method raises the EditFinished event
    void RaiseEditFinishedEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(EditTicket.EditFinishedEvent);
        RaiseEvent(newEventArgs);
        MessageBox.Show("Raised Routed");
    }

我的问题出现在主窗口侧

主窗口中的代码

  //raise editfinshed event
            RaiseEditFinishedEvent();

我的处理程序代码是这个但不是动作

 public MainWindow()
    {
        InitializeComponent();

       AddHandler(EditTicket.EditFinishedEvent, new RoutedEventHandler(RefreshEnteredTickets));
        Application.Current.MainWindow.WindowState = WindowState.Maximized;
    }

任何人都可以建议我出错的地方吗?

1 个答案:

答案 0 :(得分:1)

您尝试使用这样的简单注册:

Xaml代码(主窗口)

<Window x:Class="RoutedEventSoHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Click="ButtonBase_OnClick">Show Edit Window</Button>
</Grid>

Xaml代码(编辑故障单窗口)

<Window x:Class="RoutedEventSoHelpAttempt.EditTicket"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="EditTicket" Height="300" Width="300">
<Grid>
    <Button Content="Edit" Click="ButtonBase_OnClick"></Button>
</Grid>

代码

    /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    EditTicket editTicket=new EditTicket();

    public MainWindow()
    {
        InitializeComponent();
        Loaded += OnLoaded;
        Unloaded += OnUnloaded;
    }

    private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
    {
        Loaded -= OnLoaded;
        Unloaded -= OnUnloaded;
        editTicket.EditFinished -= RefreshEnteredTickets;

    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        editTicket.EditFinished += RefreshEnteredTickets;
    }


    private void RefreshEnteredTickets(object sender, RoutedEventArgs e)
    {

    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        editTicket.Show();
    }
}

    /// <summary>
/// Interaction logic for EditTicket.xaml
/// </summary>
public partial class EditTicket : Window
{
    //public event EventHandler EditFinished;
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent EditFinishedEvent;

    static EditTicket()
    {
        EditFinishedEvent = EventManager.RegisterRoutedEvent(
            "EditFinished", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof (EditTicket));
    }

    public EditTicket()
    {
        InitializeComponent();
    }



    // Provide CLR accessors for the event
    public event RoutedEventHandler EditFinished
    {
        add { AddHandler(EditFinishedEvent, value); }
        remove { RemoveHandler(EditFinishedEvent, value); }
    }
    // This method raises the EditFinished event
    void RaiseEditFinishedEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(EditFinishedEvent, this);
        RaiseEvent(newEventArgs);
        MessageBox.Show("Raised Routed");
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        RaiseEditFinishedEvent();
    }
}

此外,您还可以查看下一个链接: here

如果您需要更多帮助,请与我联系。

问候。