ListBox を含むユーザー コントロールがあります。
listBox.SelectionChanged イベントをラップするユーザー コントロールで、SelectionChanged イベントを公開したいと考えています。
そのため、リストボックス項目の選択が変更されると、ユーザー コントロールの独自のカスタム イベントもその後発生します...
どうすればいいですか?任意のサンプルをいただければ幸いです。ありがとう!
ListBox を含むユーザー コントロールがあります。
listBox.SelectionChanged イベントをラップするユーザー コントロールで、SelectionChanged イベントを公開したいと考えています。
そのため、リストボックス項目の選択が変更されると、ユーザー コントロールの独自のカスタム イベントもその後発生します...
どうすればいいですか?任意のサンプルをいただければ幸いです。ありがとう!
ラッピングできたとしても、ラッピングが最善のアプローチであるかどうかはわかりません。独自のイベントを定義し、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);
}
}
参考文献:
UserControl のカスタム イベントでビジュアル ツリーをバブルアップさせたい場合は、それをRoutedEventとして公開する必要があります。.xaml.cs ファイルで、イベントをルーティング イベントとして登録し、カスタム ハンドラーとイベント引数クラスを実装する必要があります。
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) { }