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