Tôi có lớp sau và muốn chuyển biến văn bản thành RoutedEventArgs.Chuyển biến với 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));
}
}
đây là mã từ Window1 đó là những lớp học chính trong một ứng dụng WPF
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);
}
}
Tôi muốn để có thể in giá trị của "String text" trong phương pháp CloseTab. Làm thế nào tôi có thể làm cho "chuỗi văn bản" được thông qua với RoutedEventArgs args?
Trân trọng!
EDIT
tôi đã thực hiện một số thay đổi cho dự án và đây là mã
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);
}
}
Sau khi thực hiện thay đổi (khi tôi mở nhiều hơn 1 tab ứng dụng bị treo), bạn sẽ truy cập "chuỗi văn bản" trong phương thức CloseTab trong lớp Window1 như thế nào?
Cảm ơn, tôi thực sự đánh giá cao iate câu hỏi này, và sửa đổi/bổ sung bạn đã thực hiện. – wonea