windows-phone-7 - 为什么我不能在不将 child 的背景设置为透明的情况下点击/单击 Border/ContentControl 内的空白区域?

标签 windows-phone-7 xaml silverlight-4.0

我终于能够基于 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 之间单击,则不会触发任何事件,也不会发生任何事情。仅当网格的背景为 Transparent 时它是否“占用空间”。

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

    最佳答案

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

    有关 HitTest 的更多信息

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

    关于windows-phone-7 - 为什么我不能在不将 child 的背景设置为透明的情况下点击/单击 Border/ContentControl 内的空白区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664251/

    相关文章:

    xaml - Silverlight 4.0表单初始化问题

    windows-phone-7 - WP7 : TextBox with suggestions

    windows-phone-7 - Windows Phone 7 嗅探器

    windows-phone-7 - 如何在 wp7 中的列表框项目之间插入特定数量的空间?

    wpf - 将标题/标题添加到上下文菜单

    unit-testing - 为 XAML 创建自定义代码分析规则

    xaml - 如何将对象从 xaml 页面传递到另一个页面?

    silverlight - 如何在 WP7 中的 Canvas 之外隐藏控件?

    c# - 只读值的优雅解决方案

    c# - Silverlight 类库到 Windows Phone 7 类库