将事件从自定义控件传播到表单
我有一个自定义控件,如:
public sealed class BorderEx : Control { public static readonly RoutedEvent ReloadClickEvent = EventManager.RegisterRoutedEvent("ReloadClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BorderEx)); public event RoutedEventHandler ReloadClick { add { AddHandler(ReloadClickEvent, value); } remove { RemoveHandler(ReloadClickEvent, value); } } void RaiseReloadClickEvent() { var newEventArgs = new RoutedEventArgs(ReloadClickEvent); RaiseEvent(newEventArgs); } static BorderEx() { DefaultStyleKeyProperty.OverrideMetadata(typeof(BorderEx), new FrameworkPropertyMetadata(typeof(BorderEx))); } }
并且在reloadButton上发生了事件,点击generic.xaml
</ControlTemplate
但我不知道如何从内部按钮引发此外部事件。 花费最后几个小时谷歌搜索,最终没有任何结果。 以上EventSetter不起作用。
这部分还可以,
public static readonly RoutedEvent ReloadClickEvent = EventManager.RegisterRoutedEvent("ReloadClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BorderEx)); public event RoutedEventHandler ReloadClick { add { AddHandler(ReloadClickEvent, value); } remove { RemoveHandler(ReloadClickEvent, value); } } static BorderEx() { DefaultStyleKeyProperty.OverrideMetadata(typeof(BorderEx), new FrameworkPropertyMetadata(typeof(BorderEx))); }
我必须创建依赖属性,所以我可以在click事件上更改一些值
public static readonly DependencyProperty ReloadProperty = DependencyProperty.Register("Reload", typeof (bool), typeof (BorderEx), new PropertyMetadata(default(bool), ReloadClicked)); public bool Reload { get { return (bool) GetValue(ReloadProperty); } set { SetValue(ReloadProperty, value); } }
我可以在更改时触发的其他方法中处理它
private static void ReloadClicked(DependencyObject o, DependencyPropertyChangedEventArgs e) { if (!((bool) e.NewValue)) return; var sender = (BorderEx) o; sender.RaiseEvent(new RoutedEventArgs(ReloadClickEvent)); }
然后它只需要改变点击值
您的事件看起来很棒,但无法在触发器中设置EventSetter
。 从链接引用:
因为使用EventSetter连接事件处理程序是一个编译时function,它通过IStyleConnector接口进行检测,所以还有另一个名为IComponentConnector的接口,XAML编译器使用该接口为独立的XAML元素连接事件处理程序。
你可以这样做。 识别EventSetter
外部触发器,例如在早期Style
/ Template
:
Code behind
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void ReloadClickEvent(object sender, RoutedEventArgs e) { RaiseEvent(new DemoEventArgs(BorderEx.ReloadClickEvent, sender)); } } public class DemoEventArgs : RoutedEventArgs { public DemoEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { MessageBox.Show("Raise!"); } } public sealed class BorderEx : Control { public static readonly RoutedEvent ReloadClickEvent = EventManager.RegisterRoutedEvent("ReloadClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BorderEx)); public event RoutedEventHandler ReloadClick { add { AddHandler(ReloadClickEvent, value); } remove { RemoveHandler(ReloadClickEvent, value); } } private void RaiseReloadClickEvent() { var newEventArgs = new RoutedEventArgs(ReloadClickEvent); RaiseEvent(newEventArgs); } static BorderEx() { DefaultStyleKeyProperty.OverrideMetadata(typeof(BorderEx), new FrameworkPropertyMetadata(typeof(BorderEx))); } }
或者,使用DependencyProperty
(也可以附加 )。 例:
属性定义:
public static readonly DependencyProperty SampleProperty = DependencyProperty.RegisterAttached("Sample", typeof(bool), typeof(SampleClass), new UIPropertyMetadata(false, OnSample)); private static void OnSample(DependencyObject sender, DependencyPropertyChangedEventArgs e) { if (e.NewValue is bool && ((bool)e.NewValue) == true) { // do something... } }
在XAML中调用。
在EventTrigger
:
True
使用DataTrigger
(在Style
/ DataTemplate
/ etc
):
使用Trigger
( Style
):
使用后面的代码:
private void Clear_Click(object sender, RoutedEventArgs e) { SampleClass.SetSampleClass(MyBox, true); }