2

私のアプリケーションには、いくつかのボタンがあるデータテンプレートがあります。ページごとに異なるアクションが必要なため、これらのボタンのハンドラーをApplication.xaml.vb / csファイルではなく現在のページ(多くのページでこのテンプレートを使用しています)で起動するようにします。

はっきりしているといいのですが。

4

2 に答える 2

2

コマンドを使用してこれを実現できます。実行固有のButtonsにsを含めます。DataTemplateCommand

<Button Command="{x:Static MyCommands.SomeCommand}"/>

次に、それを使用する各ビューで:を処理DataTemplateします。Command

<UserControl>
    <UserCommand.CommandBindings>
         <CommandBinding Command="{x:Static MyCommands.SomeCommand}"
                         Executed="_someHandler"/>
    </UserCommand.CommandBindings>
</UserControl>

コメント後に編集:これらの手順に従ってResourceDictionaryコードビハインドを作成したら、通常の方法でイベントを接続するだけです。

MyResources.xaml

<ListBox x:Key="myListBoxResource" ItemSelected="_listBox_ItemSelected"/>

次にMyResources.xaml.cs

private void _listBox_ItemSelected(object sender, EventArgs e)
{
    ...
}
于 2009-03-30T13:56:56.937 に答える
0

コマンドではなくイベントを使用する場合は、Clickイベント ハンドラーに次のように記述します。

private void Button_Click(object sender, RoutedEventArgs e)
{
    var dataItem = (FrameworkElement)sender).DataContext;
    // process dataItem
}
于 2015-06-09T08:57:16.253 に答える