Passing variable with RoutedEventArgs?

别来无恙 提交于 2019-11-27 18:07:09

问题


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

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

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?

Best Regards!

EDIT

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

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

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

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

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?


回答1:


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).

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.




回答2:


I needed something similar and with the help of both @Arya and @H.B. I came up with this:

My custom RoutedEventArgs subclass

public class ChangePageEventArgs : RoutedEventArgs {
    private readonly int _pageNumber;

    public int PageNumber {
        get { return _pageNumber; }
    }

    public ChangePageEventArgs(RoutedEvent routedEvent, int pageNumber) : base(routedEvent) {
        this._pageNumber = pageNumber;
    }
}

My child class

private void defineButton_Click(object sender, RoutedEventArgs e) {
    ChangePageItemList(2);
}

public static readonly RoutedEvent GoToItemPropertiesViewEvent = EventManager.RegisterRoutedEvent(
"GoToItemPropertiesView", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NoObjectView));

// Provide CLR accessors for the event
public event RoutedEventHandler GoToItemPropertiesView {
    add { AddHandler(GoToItemPropertiesViewEvent, value); }
    remove { RemoveHandler(GoToItemPropertiesViewEvent, value); }
}

public void ChangePageItemList(int _pageNumber) {
    ChangePageEventArgs args = new ChangePageEventArgs(GoToItemPropertiesViewEvent, _pageNumber);
    this.RaiseEvent(args);
}

My parent class

private ItemListView createItemListView() {
    _itemListView = new ItemListView();
    _itemListView.GoToItemPropertiesView += new RoutedEventHandler(ChangePage);

    return _itemListView;
}

private void ChangePage(object sender, RoutedEventArgs e) {
    ChangePageEventArgs args = (ChangePageEventArgs)e;
    // Value of page number is 2
    int _pageNumber = args.PageNumber;
}

Hope it helps you @Arya




回答3:


1 - create a new event data class:

public class SelectedRowEventArgs : RoutedEventArgs
{
    public int Value { get; set; }

    public SelectedRowEventArgs(RoutedEvent routedEvent, int value) : base(routedEvent)
    {
        this.Value = value;
    }
}

2 - create links code in a user control:

public partial class AusenciasBaseAdmin : UserControl
{

    public static readonly RoutedEvent RowChangedEvent = EventManager.RegisterRoutedEvent("RowChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AusenciasBaseAdmin));

    public event RoutedEventHandler RowChanged
    {
        add { AddHandler(RowChangedEvent, value); } remove { RemoveHandler(RowChangedEvent, value); }
    }



    public AusenciasBaseAdmin()
    {
        InitializeComponent();
    }


    private void RegAusencias_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.RaiseEvent(new SelectedRowEventArgs(RowChangedEvent, 1));
    }
}

3 - link the user control in a main form:

<control:AusenciasBaseAdmin Grid.Row="0" Grid.Column="0"
                        x:Name="GridAusencias" 
                        RowChanged="GridAusencias_RowChanged_1"
                        />

4 - behind code in a main form:

    private void GridAusencias_RowChanged_1(object sender, RoutedEventArgs e)
    {
        SelectedRowEventArgs t = (SelectedRowEventArgs)e;

        MessageBox.Show(t.Value.ToString());
    }

CONGRATULATIONS FOR WPF TEAM. YOU HAVE ACHIEVED TO DO IT EXTREMELY COMPLEX



来源:https://stackoverflow.com/questions/8109358/passing-variable-with-routedeventargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!