[英]Xaml (Windows Phone 8 & Silverlight) usercontrol stacking issue
[英]Custom events in XAML on my UserControl on Windows Phone 7
我正在Windows Phone 7中创建UserControl,我希望当用户单击“确定”按钮时,使用我的UserControl的其他XAML可以添加与之相关的事件。
使用一个例子,它是这样的:
我有我的MainPage.xaml,我在那里使用我的UserControl,所以它是这样的:
<local:MyUserControl Canvas.Top="0" x:Name="lSelector" Width="480" Height="800" Value="0000"/>
Value只是我创建的DependencyProperty。 我想要的是能够做这样的事情:
<local:MyUserControl Canvas.Top="0" x:Name="lSelector" Width="480" Height="800" Value="0000" ValueChanged="lSelector_ValueChanged"/>
我怎样才能做到这一点?
将事件添加到您的UserControl,如下面的代码,它将显示为正常事件
public partial class UserControl1 : UserControl
{
public delegate void ValueChangedEventHandler(object sender, EventArgs e);
public event ValueChangedEventHandler ValueChanged;
public UserControl1()
{
// Required to initialize variables
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (ValueChanged != null)
{
ValueChanged(this, EventArgs.Empty);
}
}
}
然后只需订阅它
private void UserControl1_ValueChanged(object sender, System.EventArgs e)
{
// TODO: Add event handler implementation here.
}
尝试使用DependencyPropertyChangedEventHandler。
IE浏览器。
public event DependencyPropertyChangedEventHandler SelectionChanged
{
add
{
}
remove
{
}
}
因此,您应该在自定义xaml控件中看到选择已更改事件
使用RoutedEvent
:
public class Callbackable : ContentControl
{
public static readonly RoutedEvent CommandExecutedEvent = EventManager.RegisterRoutedEvent(
"CommandExecuted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Callbackable));
// Provide CLR accessors for the event
public event RoutedEventHandler CommandExecuted
{
add { AddHandler(CommandExecutedEvent, value); }
remove { RemoveHandler(CommandExecutedEvent, value); }
}
}
用法:
<local:Callbackable>
<Button local:Callbackable.CommandExecuted="Button_Executed" >
Click me
</Button>
</local:Callbackable>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.