【发布时间】:2013-03-02 00:48:59
【问题描述】:
我遇到了一个问题,即带有路径内容的按钮仅检测鼠标在路径上的点击。对于 ux,我想在按钮的任何位置注册点击。我已将按钮的背景设置为 null 和透明,因此顶部控件容器决定了背景样式。
这是另一个 SO 帖子:Mouse event on transparent background
如前所述,到目前为止,我已经尝试过透明和 null。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfMyCustomControl">
<ControlTemplate x:Key="IconTemplate" TargetType="{x:Type ContentControl}">
<Grid>
<Path Name="ForegroundSymbol" Data="M0,0 L1,0 1,1 0,1 0.5,0.5 z" Fill="{TemplateBinding Foreground}" Stretch="Fill" />
</Grid>
</ControlTemplate>
<Style x:Key="IconButtonStyle" TargetType="{x:Type RepeatButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Grid>
<ContentControl Name="icon" Template="{StaticResource IconTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="MyCustomTemplate" TargetType="{x:Type local:MyCustomControl}">
<Grid Name="LayoutRoot" Background="Red">
<RepeatButton Background="{x:Null}" Style="{StaticResource ResourceKey=IconButtonStyle}" />
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="Template" Value="{StaticResource ResourceKey=MyCustomTemplate}" />
</Style>
</ResourceDictionary>
如果我从“样式”中删除“x:Key”属性,控件就会呈现。我已经能够使用上述 xaml 控件样式重现该问题,其中点击检测不会在按钮的“背景”部分触发。
【问题讨论】:
-
如果您提供一些代码供我们查看,将会非常有帮助。
-
iconstyle怎么样?任何问题很可能与控制模板的VisualTree有关。 -
我无法使用按钮、您的
<Path>和按钮的{x:Null}背景来重现此内容。这证明是您的Style造成了这种情况。开始解构样式直到问题消失,或者反过来开始:从空白样式开始并添加东西直到问题出现。无论哪种方式,您都可以准确找到导致问题的原因,进而帮助您解决问题。
标签: wpf custom-controls