0

我正在尝试使用以下方法制作自己的复合控件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");
        }
    }
}

但是,即使我设置HandlerTrue,按钮仍然得到它。我想,它首先得到它。

然后我认为按钮上的设置IsHitTestVisibleFalse做到这一点。这一次,既没有contentGrid_ManipulationStarted,也没有MyCompositeControl.OnManipulationStarted被调用。

谢谢!

4

1 回答 1

0

显然,这非常容易。只需考虑这样一个事实,即将Grid元素一个接一个地堆叠起来,并且Background设置为Transparent 接收输入事件的元素。

这是ResourceDictionary: 简单地添加了Border控件:

<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>
                        <Button IsHitTestVisible="False">Click me</Button>
                        <Border Background="Transparent" />
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

C#文件实际上是简化的:

namespace SvetlinAnkov.examples
{
    public class MyCompositeControl : Control
    {
        public MyCompositeControl()
        {
            DefaultStyleKey = typeof(MyCompositeControl);
        }

        protected override void OnManipulationStarted(
            ManipulationStartedEventArgs e)
        {
            Debug.WriteLine("OnManipulationStarted");
        }
    }
}
于 2013-04-28T17:17:44.287 回答