通过RoutedEventArgs传递变量? [英] Passing variable with RoutedEventArgs?

查看:429
本文介绍了通过RoutedEventArgs传递变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程,想将文本变量作为RoutedEventArgs传递.

I have the following class, and want to pass the text variable as RoutedEventArgs.

  public class CloseableTabItem : TabItem
  {
    String text;

    public CloseableTabItem()
    {
      //This style is defined in themes\generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
          new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
    }

    public CloseableTabItem(String incomingText)
    {
      //This style is defined in themes\generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
          new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
      text = incomingText;
    }

    public static readonly RoutedEvent CloseTabEvent =
        EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(CloseableTabItem));

    public event RoutedEventHandler CloseTab
    {
      add { AddHandler(CloseTabEvent, value); }
      remove { RemoveHandler(CloseTabEvent, value); }
    }

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      Button closeButton = base.GetTemplateChild("PART_Close") as Button;
      if (closeButton != null)
        closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
    }

    void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
    }
  }

这是Window1中的代码,它是WPF应用程序中的主要类

this is the code from Window1 which is the main class in a WPF app

  public partial class Window1 : Window
  {
    public static Window1 myWindow1;

    public Window1()
    {
      myWindow1 = this;
      InitializeComponent();
      this.AddHandler(CloseableTabItem.CloseTabEvent, new RoutedEventHandler(this.CloseTab));
    }

    private void CloseTab(object source, RoutedEventArgs args)
    {
      TabItem tabItem = args.Source as TabItem;
      if (tabItem != null)
      {
        TabControl tabControl = tabItem.Parent as TabControl;
        if (tabControl != null)
          tabControl.Items.Remove(tabItem);
      }
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
      CloseableTabItem tabItem = new CloseableTabItem("THIS IS A TEST");
      MainTab.Items.Add(tabItem);
    }
  }

我希望能够在CloseTab方法中打印字符串文本"的值.如何使RoutedEventArgs args传递字符串文本"?

I want to be able to print the value of "String text" in the CloseTab method. How can I make "String text" be passed with RoutedEventArgs args?

最诚挚的问候!

编辑

我对项目进行了一些更改,这是代码

I made some changes to the project and here is the code

ClosableTabItem.cs

ClosableTabItem.cs

namespace SampleTabControl
{
  public class CloseableTabItem : TabItem
  {

    String text;
    public delegate void TabsEventHandler(object sender, TabsEventArgs e);

    public CloseableTabItem()
    {
      //This style is defined in themes\generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
          new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
    }

    public CloseableTabItem(String incomingText)
    {
      //This style is defined in themes\generic.xaml
      DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
          new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
      this.text = incomingText;
    }

    public static readonly RoutedEvent CloseTabsEvent = EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble, typeof(TabsEventHandler), typeof(CloseableTabItem));    

    public event TabsEventHandler CloseTab
    {
      add { AddHandler(CloseTabsEvent, value); }
      remove { RemoveHandler(CloseTabsEvent, value); }
    }


    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      Button closeButton = base.GetTemplateChild("PART_Close") as Button;
      if (closeButton != null)
        closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
    }

    void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      TabsEventArgs args = new TabsEventArgs(CloseTabsEvent, text);
      RaiseEvent(args);
    }
  }
}

TabsEventArgs.cs

TabsEventArgs.cs

public class TabsEventArgs : RoutedEventArgs
{
    private readonly string text;

    public string Text
    {
        get { return text; }
    }

    public TabsEventArgs(RoutedEvent routedEvent, string text) : base(routedEvent)
    {
        this.text = text;
    }
}

Window1.cs

Window1.cs

  public partial class Window1 : Window
  {
    public static Window1 myWindow1;

    public Window1()
    {
      myWindow1 = this;
      InitializeComponent();
      this.AddHandler(CloseableTabItem.CloseTabsEvent, new RoutedEventHandler(this.CloseTab));
    }

    private void CloseTab(object source, RoutedEventArgs args)
    {      
      TabItem tabItem = args.Source as TabItem;
      if (tabItem != null)
      {
        TabControl tabControl = tabItem.Parent as TabControl;
        if (tabControl != null)
          tabControl.Items.Remove(tabItem);
      }
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
      CloseableTabItem tabItem = new CloseableTabItem("THIS IS A TEST");
      MainTab.Items.Add(tabItem);
    }
  }

进行更改后(当我打开多个选项卡时,应用程序崩溃),如何在Window1类的CloseTab方法中访问字符串文本"?

After making the changes (when I open more than 1 tab the app crashes), how would you access the "string text" in the CloseTab method in Window1 class?

推荐答案

创建RoutedEventArgs的新子类,向其中添加属性,您可以在其中存储要传递的变量,并创建类型分别为,然后将其分配为事件的处理程序类型(当前使用常规的RoutedEventHandler,因此仅提供常规的RoutedEventArgs).

Create a new subclass of RoutedEventArgs, add a property to it where you can store the variable to be passed and create a respective handler delegate of type void (object, YourNewEventArgs) which you then assign as the handler type of your event (which currently uses a normal RoutedEventHandler which hence only provides normal RoutedEventArgs).

如果要引发该事件,则需要创建新的事件args并将变量传递给该变量的属性.如何在处理程序中获取值应该是不言自明的.

If the event then is to be raised you need to create your new event args and pass the variable to its property for that variable. How to get the value in the handler should be self-explanatory.

这篇关于通过RoutedEventArgs传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆