WPFでは、ItemsControl ControlTemplateでイベントをどのように処理できますか

StackOverflow https://stackoverflow.com/questions/1408065

質問

ItemsControl ControlTemplate内のイベントを処理しようとしています。ボタンのMouseUpイベントとMouseDownイベントを割り当てました(btnRight以下)。問題は、ボタンをクリックしても、イベントがコードビハインドに到達しないことです。 ControlTemplatesのイベントはどのように機能し、これをフックするには何をする必要がありますか? OnApplyTemplateイベントの実行中にコードビハインドのボタンにイベントを割り当てようとしました。

ご協力ありがとうございます!

<ItemsControl.Template>
    <ControlTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="36" />
                <ColumnDefinition />
                <ColumnDefinition Width="36" />
            </Grid.ColumnDefinitions>
            <Button x:Name="btnLeft" Grid.Column="0" Height="36">
                <Button.Template>
                    <ControlTemplate>
                        <Image>
                            <Image.Source>
                                <BitmapImage UriSource="Images\left.png" />
                            </Image.Source>
                        </Image>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Border Grid.Column="1" BorderBrush="Black" BorderThickness="1" Background="Black" Padding="6">
                <ItemsPresenter Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MarginOffset}" />
            </Border>
            <Button x:Name="btnRight" Grid.Column="2" Height="36" MouseUp="btnRight_MouseUp" MouseDown="btnRight_MouseDown">
                <Button.Template>
                    <ControlTemplate>
                        <Image>
                            <Image.Source>
                                <BitmapImage UriSource="Images\right.png" />
                            </Image.Source>
                        </Image>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Grid>
    </ControlTemplate>
</ItemsControl.Template>
役に立ちましたか?

解決

ボタンクリックイベントを使用する代わりに、新しいコマンドを作成し、ボタンのCommandプロパティを作成したコマンドにバインドしてから、実行時にコマンドを処理するためにCommandBindingをユーザーコントロールに追加します。

こちら for の詳細情報。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top