在 WPF 中,如何处理 ItemsControl ControlTemplate 中的事件

发布于 2024-08-04 00:16:43 字数 2037 浏览 0 评论 0原文

我正在尝试处理 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>

I'm trying to handle an event inside an ItemsControl ControlTemplate. I have assigned the MouseUp and MouseDown events of a button (btnRight below). The problem is that when I click on the button, the event never reaches my code-behind. How do events in ControlTemplates work and what do I need to do to hook this up? I've tried assigning the events to the button in code-behind during the OnApplyTemplate event to no avail.

Thanks for your help!

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2024-08-11 00:16:43

不要使用按钮单击事件,而是创建一个新命令,将按钮的 Command 属性绑定到您创建的命令,然后将 CommandBinding 添加到用户控件以在执行命令时处理该命令。

请参阅此处 了解更多信息。

Instead of using button click events, create a new Command, bind the Command property of the Button to the Command you created, and then add a CommandBinding to your user control to handle the command when it is executed.

See here for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文