如何用我自己的事件包装标准的wpf事件?

时间:2012-04-27 21:27:23

标签: wpf wpf-controls

我有一个包含ListBox的用户控件。

我想在我的用户控件上公开一个SelectionChanged事件,该事件包装了listBox.SelectionChanged事件。

因此,当列表框项目选择发生更改时,用户控件上的我自己的自定义事件也会在此之后被触发...

我该怎么做? 任何样品将不胜感激。 谢谢!

2 个答案:

答案 0 :(得分:1)

我不确定包装是最好的方法,即使你可以包装它。我建议只定义你自己的事件,并在挂钩到listBox.SelectionChanged的处理程序中激活你自己的事件。然后,您可以将原始列表框事件中的任何数据传递给您自己的事件。

添加了示例:

public partial class MainWindow : Window
{
    public delegate void CustomSelectionChangedEventHandler(object sender, SelectionChangedEventArgs e);
    public event CustomSelectionChangedEventHandler CustomSelectionChanged;


    public MainWindow()
    {
        InitializeComponent();
        listBox1.SelectionChanged += delegate(object sender, SelectionChangedEventArgs e)
        {
            OnCustomSelectionChanged(e);
        };

    }

    void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        OnCustomSelectionChanged(e);
    }

    //We'll use the system defined SelectionChangedEventArgs type instead of creating a derived EventArgs class
    protected virtual void OnCustomSelectionChanged(SelectionChangedEventArgs e)
    {
        if (CustomSelectionChanged != null)
            CustomSelectionChanged(this, e);

    }
}

进一步阅读:

答案 1 :(得分:1)

如果您希望UserControl上的自定义事件冒泡出可视树,则应将其公开为RoutedEvent。在.xaml.cs文件中,您需要将事件注册为路由事件,然后实现自定义处理程序和事件args类。

XAML:

<UserControl x:Class="WpfApplication1.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
       <ListView Name="myListView" SelectionChanged="OnSelectionChanged_"/>
    </Grid>
</UserControl>

代码:

public partial class MyUserControl : UserControl
{
    public delegate void CustomSelectionChangedEventHandler(object sender, SelectionChangedRoutedEventArgs args);


    public static readonly RoutedEvent CustomSelectionChangedEvent = EventManager.RegisterRoutedEvent(
        "CustomSelectionChanged", RoutingStrategy.Bubble, typeof(CustomSelectionChangedEventHandler), typeof(MyUserControl));


    public event RoutedEventHandler CustomSelectionChanged
    {
        add { AddHandler(CustomSelectionChangedEvent, value); }
        remove { RemoveHandler(CustomSelectionChangedEvent, value); }
    }


    public MyUserControl()
    {
        InitializeComponent();
    }


    private void OnSelectionChanged_(object sender, SelectionChangedEventArgs e)
    {
        RaiseEvent(new SelectionChangedRoutedEventArgs(myListView, CustomSelectionChangedEvent, e.AddedItems, e.RemovedItems)); 
    }
}


public class SelectionChangedRoutedEventArgs : RoutedEventArgs
{
    public IList AddedItems { get; set; }
    public IList RemovedItems { get; set; }

    public SelectionChangedRoutedEventArgs(object source, RoutedEvent routedEvent, IList addedItems, IList removedItems) 
        : base(routedEvent, source)
    {
        AddedItems = addedItems;
        RemovedItems = removedItems;
    }
}

然后,您的控件的调用者将为CustomSelectionChanged事件提供一个事件处理程序,其签名为:

private void OnCustomSelectionChanged(object sender, SelectionChangedRoutedEventArgs e) { }