我正在尝试使用以下方法制作自己的复合控件ControlTemplate
:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:svetlin="clr-namespace:SvetlinAnkov.examples">
<Style TargetType="svetlin:MyCompositeControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="svetlin:MyCompositeControl">
<Grid x:Name="ContentGrid">
<Button>Click me</Button>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
而且我想“拦截”触摸事件,以便它们不是由按钮处理,而是由MyCompositeControl
:
namespace SvetlinAnkov.examples
{
public class MyCompositeControl : Control
{
private Grid contentGrid;
public MyCompositeControl()
{
DefaultStyleKey = typeof(MyCompositeControl);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
contentGrid = GetTemplateChild("ContentGrid") as Grid;
contentGrid.ManipulationStarted +=
new EventHandler<ManipulationStartedEventArgs>(
contentGrid_ManipulationStarted);
// The same for Delta & Completed
}
protected override void OnManipulationStarted(
ManipulationStartedEventArgs e)
{
Debug.WriteLine("OnManipulationStarted");
}
private void contentGrid_ManipulationStarted(
object sender, ManipulationStartedEventArgs e)
{
e.Handled = true;
Debug.WriteLine("ManipulationStarted");
}
}
}
但是,即使我设置Handler
为True
,按钮仍然得到它。我想,它首先得到它。
然后我认为按钮上的设置IsHitTestVisible
会False
做到这一点。这一次,既没有contentGrid_ManipulationStarted
,也没有MyCompositeControl.OnManipulationStarted
被调用。
谢谢!