在 WPF 中,如何处理 ItemsControl ControlTemplate 中的事件
我正在尝试处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用按钮单击事件,而是创建一个新命令,将按钮的 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.