windows-phone-7 – 为什么我不能点击/点击Border / ContentControl中的空白区域而不将孩子的背景设置为透明?

我终于能够基于ContentControl创建一个“简单”的透明按钮控件.但是,有人可以解释为什么我不能点击/点击控件的任何空白区域,直到我将子元素的背景设置为透明?在以下情况下我也遇到了这个问题:

>我试图使用Border
>我设置按钮的ControlTemplate而不是ContentTemplate.

这是我的“按钮”类:

public class TransparentButton : ContentControl {
    public TransparentButton() {            
        HorizontalContentAlignment = HorizontalAlignment.Stretch;
    }

    public override void OnApplyTemplate() {
        var child = Content as Grid;

        if (child != null) {
            child.Background = new SolidColorBrush(Colors.Transparent);
        }

        base.OnApplyTemplate();
    }
}

这对我使用时的情况非常具体(假设一个Grid子),但它有效.我使用它的原因是启用了TiltEffect的列表(非ListBox).

问题的背景:

<ItemsControl x:Name="Items" toolkit:TiltEffect.IsTiltEnabled="True">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <controls:TransparentButton 
                cal:Message.Attach="[Event Tap] = [Action Go($dataContext)]">
                <Grid>
                    <StackPanel HorizontalAlignment="Left">
                        <TextBlock Text="{Binding Test}" />                            
                    </StackPanel>
                    <StackPanel HorizontalAlignment="Right">
                        <TextBlock Text="{Binding Test2}" />                            
                    </StackPanel>
                </Grid>
            </controls:TransparentButton>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果在项目内的StackPanel之间单击,则不会触发任何事件,也不会发生任何事件.只有当网格的背景是透明的时候才会“占用空间”.

我来自网络背景,所以这很令人困惑;即使没有设置背景,包含元素也应该“可以测试”.

没有背景的对象通常在XAML术语中称为空心或不可命中.因此必须设置背景以使对象响应命中.要实现透明对象的命中测试,应将背景设置为透明.

有关命中测试的更多信息

http://msdn.microsoft.com/en-us/library/ms752097.aspx

相关文章

windows无法连接到wifi怎么办
文章浏览阅读2.2k次,点赞6次,收藏20次。在我们平时办公工作...
文章浏览阅读1k次。解决 Windows make command not found 和...
文章浏览阅读3.2k次,点赞2次,收藏6次。2、鼠标依次点击“计...
文章浏览阅读1.3w次。蓝光版属于高清版的一种。BD英文全名是...
文章浏览阅读974次,点赞7次,收藏8次。提供了更强大的功能,...