在我的模板控件中,我有一个按钮和一个椭圆。我用 Interaction.Behavior 添加了点击事件。按钮工作正常。但 Ellipse 从不触发该事件。为什么 ?
<Style TargetType="local:SensorMeter">
<Setter Property="UseSystemFocusVisuals" Value="True"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:SensorMeter">
<Viewbox>
<Ellipse x:Name="PART_MeterCenter" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Center" VerticalAlignment="Center">
<Ellipse.RenderTransform>
<CompositeTransform/>
</Ellipse.RenderTransform>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CenterCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CenterCommandParameter}"/>
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Ellipse>
<Button Content="ClickBtn">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CenterCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CenterCommandParameter}"/>
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Button>.........
我在 TemplateControl C# 代码中使用此代码绘制圆圈。
var _meterCenter = _sensorMeter.GetTemplateChild("PART_MeterCenter") as Ellipse;
if (_meterCenter != null)
{
_meterCenter.Stroke = _sensorMeter.MeterBorder_Brush;
_meterCenter.StrokeThickness = _sensorMeter.MeterBorderWidth;
_meterCenter.Fill = _sensorMeter.CenterThresHoldColor;
_meterCenter.Opacity = Opacity;
_meterCenter.Height = _sensorMeter.meterCenterSize * 2;
_meterCenter.Width = _sensorMeter.meterCenterSize * 2;
_meterCenter.Margin = new Thickness(0, _sensorMeter.MeterYOffset*2, 0, 0);
}
实际上,在我的代码中,有很多网格、路径、椭圆。我应该去最上层接受点击事件吗?但按钮可以在相同的 z-index 中触发事件。
在我的代码中,没有画布。这里只有 Grid、Path、Ellipse。