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