我的UserControl中有一个简单的TabControl,它嵌入在父控件中,如下所示:
<StackPanel>
<myviews:myusercontrol></myviews:myusercontrol>
</StackPanel>
在myusercontrol中,我有以下实现:
public partial class MyUserControl
{
public delegate void SelectedTabItemHandler(string selectedTabItem);
public event SelectedTabItemHandler TabItemSelected;
public MyUserControl()
{
InitializeComponent();
}
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(TabItemSelected != null)
{
TabItemSelected("some selected Item");
}
}
}
如何在我的XAML中连接TabItemSelected事件,如下所示:
<myviews:myusercontrol TabItemSelected="TabSelectionChanged"></myviews:myusercontrol>
更新2:
为了传递自定义参数,我正在做这样的事情:
public partial class PatientEditTabView : UserControl
{
// Routed Event
public static readonly RoutedEvent SelectedTabItemEvent = EventManager.RegisterRoutedEvent("TabItemSelected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PatientEditTabView));
public PatientEditTabView()
{
InitializeComponent();
}
public event RoutedEventHandler TabItemSelected
{
add { AddHandler(SelectedTabItemEvent, value); }
remove { RemoveHandler(SelectedTabItemEvent, value); }
}
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedTabRoutedEventArgs eventArgs = new SelectedTabRoutedEventArgs(PatientEditTabView.SelectedTabItemEvent,tabControl.SelectedValue as String);
RaiseEvent(eventArgs);
}
}
更新3:
public class SelectedTabRoutedEventArgs : RoutedEventArgs
{
private readonly String _selectedItem;
public SelectedTabRoutedEventArgs(RoutedEvent routedEvent,string selectedItem)
: base(routedEvent)
{
_selectedItem = selectedItem;
}
public string SelectedItem
{
get { return _selectedItem; }
}
}
答案 0 :(得分:4)
如果您想从XAML挂钩处理程序,则需要创建 custom Routed event
。
请参阅此处的文章 - How to create custom routed event.
public partial class MyUserControl
{
public static readonly RoutedEvent TabItemSelectedEvent =
EventManager.RegisterRoutedEvent("TabItemSelected",
RoutingStrategy.Bubble, typeof(RoutedEventHandler),
typeof(MyUserControl));
public event RoutedEventHandler TabItemSelected
{
add { AddHandler(TabItemSelectedEvent, value); }
remove { RemoveHandler(TabItemSelectedEvent, value); }
}
void RaiseTabItemSelectedEvent()
{
RoutedEventArgs newEventArgs =
new RoutedEventArgs(MyUserControl.TabItemSelectedEvent);
RaiseEvent(newEventArgs);
}
private void TabControl_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
RaiseTabItemSelectedEvent();
}
}
注意 :您需要创建从RoutedEventArgs派生的类,以防您希望在您的案例中传递一些自定义数据(如字符串)并在提升时传递自定义事件参数事件
<强>更新强>:
就像我说的,你必须从RoutedEventArgs创建派生类,以防想要传递字符串值。这就是它的方式:
public class SelectedTabRoutedEventArgs : RoutedEventArgs
{
private readonly string selectedItem;
public SelectedTabRoutedEventArgs(RoutedEvent routedEvent,
string selectedItem)
:base(routedEvent)
{
this.selectedItem = selectedItem;
}
public string SelectedItem
{
get
{
return selectedItem;
}
}
}
并更新其他方法:
void RaiseTabItemSelectedEvent(string selectedItem)
{
SelectedTabRoutedEventArgs newEventArgs =
new SelectedTabRoutedEventArgs(SampleUserControl.TabItemSelectedEvent,
selectedItem);
RaiseEvent(newEventArgs);
}
private void TabControl_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
RaiseTabItemSelectedEvent(tabControl.SelectedValue.ToString());
}
此外,您在窗口中的目标事件处理程序将如下所示:
private void TabSelectionChanged(object sender, RoutedEventArgs e)
{
SelectedTabRoutedEventArgs args = (SelectedTabRoutedEventArgs)e;
string selectedItem = args.SelectedItem;
}