我有以下类,并希望将文本变量作为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应用程序中的主要类
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方法中打印“String text”的值。如何使用RoutedEventArgs args传递“String text”?
最诚挚的问候!
修改
我对项目进行了一些更改,这是代码
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);
}
}
进行更改后(当我打开超过1个选项卡时应用程序崩溃),您将如何访问Window1类中CloseTab方法中的“字符串文本”?
答案 0 :(得分:10)
创建一个新的RoutedEventArgs
子类,向其添加一个属性,您可以在其中存储要传递的变量,并创建一个类型为void (object, YourNewEventArgs)
的相应处理程序委托,然后将其指定为处理程序类型您的活动(目前使用正常的RoutedEventHandler
,因此只能提供正常的RoutedEventArgs
)。
如果要引发事件,则需要创建新事件args并将变量传递给该变量的属性。如何在处理程序中获取值应该是不言自明的。
答案 1 :(得分:9)
在@Arya和@H.B的帮助下,我需要类似的东西。我想出了这个:
我的自定义RoutedEventArgs子类
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;
}
}
我的孩子班
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);
}
我的父类
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;
}
希望它能帮助你@Arya
答案 2 :(得分:0)
1-创建一个新的事件数据类:
public class SelectedRowEventArgs : RoutedEventArgs
{
public int Value { get; set; }
public SelectedRowEventArgs(RoutedEvent routedEvent, int value) : base(routedEvent)
{
this.Value = value;
}
}
2-在用户控件中创建链接代码:
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-以主要形式链接用户控件:
<control:AusenciasBaseAdmin Grid.Row="0" Grid.Column="0"
x:Name="GridAusencias"
RowChanged="GridAusencias_RowChanged_1"
/>
4-主要形式的代码后方:
private void GridAusencias_RowChanged_1(object sender, RoutedEventArgs e)
{
SelectedRowEventArgs t = (SelectedRowEventArgs)e;
MessageBox.Show(t.Value.ToString());
}
WPF团队的祝贺。您已经做到了极其复杂