2012-04-27 11 views
0

私はListBoxを含むユーザーコントロールを持っています。標準のwpfイベントを自分のイベントでラップするにはどうすればよいですか?

ListBox.SelectionChangedイベントをラップするユーザーコントロール上にSelectionChangedイベントを公開したいとします。

たときに、リストボックスの項目選択の変更、ユーザーコントロールに独自のカスタムイベントはその後も解雇ます、だから...

私はそれをどのように行うのでしょうか? いずれのサンプルもありがとうございます。 ありがとう!

答えて

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

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> 

はコード:

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

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イベントのイベントハンドラを提供します

関連する問題